我有一个显示列表的 MainActivity。但是当用户点击状态栏中的通知时,我想在 MainActivity 顶部显示一个对话框。是否可以查询活动是否通过通知启动?
我还尝试向意图添加额外信息,让活动知道它应该弹出该对话框。但捆绑始终为空。并将其添加到 onResume() 没有意义,因为 Activity 可能已经可见。
long id = intent.getLongExtra("id", 0);
String title = intent.getStringExtra("msg");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_action_ic_action_edit);
mBuilder.setContentTitle("EasyReminder");
mBuilder.setContentText(title);
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.putExtra("ShowDialog", true);
PendingIntent pi = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify((int)id, mBuilder.build());
MainActivity.java
@Override
public void onResume() {
super.onResume();
// this is always null
Bundle extras = getIntent().getExtras();
if(extras != null) {
DialogPopupFragment fr = new DialogPopupFragment();
fr.show(getFragmentManager(), "DialogPopupFragment");
}
}