【问题标题】:Android: What Intent Flag to use so undestroyed Activity will not automatically show upon NotificationAndroid:在通知时不会自动显示使用什么意图标志这样未破坏的活动
【发布时间】:2012-03-18 20:36:24
【问题描述】:

我有 3 个活动

A. MainActivity - 只是一个带有文本和按钮的 Activity

B. SettingsActivity - 如果单击 (A) 上的按钮,将显示此活动,这将初始化时间选择器,当它发出警报时将发送通知

C. DisplayNotification - 用于在状态栏中显示通知的活动,单击时应显示 A

这就是我想要发生的事情:

  1. 我启动我的应用程序,将显示 A
  2. 点击按钮,会显示B
  3. 我设置了闹钟时间
  4. 触发警报时,状态栏中会显示通知
  5. 如果我点击通知,将显示 A

这就是发生的事情(我有 2 个结果不同的场景):

第一个场景:

  1. 在第 3 步之后,我点击返回按钮,现在将显示 A
  2. 然后,我再次点击返回按钮,将调用 A 的 onDestroy,屏幕将显示手机的“主页”或“桌面”
  3. 我等到警报触发
  4. 触发警报时,状态栏上会显示通知
  5. 我点击通知,它会启动我的应用并显示 A
  6. 故事结束。这是预期会发生的事情

第二种情况:(有车的)

  1. 在第3步之后,我点击HOME按钮,A的onDestroy还没有被调用,最新的屏幕是B
  2. 屏幕显示手机的“主页”或“桌面”
  3. 我等到警报触发
  4. 当警报触发时,通知会显示在状态栏上,并且 B 会自动显示而无需我点击状态栏上的通知
  5. 我想在这里发生的是它不应该自动显示 B 活动。

我是否缺少通知或意图的任何标志?

这里是触发警报的Activity B的代码sn-p

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, DisplayNotification.class);
i.putExtra("NotifID", 1);
PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, displayIntent);

这是活动 C 的代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int notifID = getIntent().getExtras().getInt("NotifID");
    Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    i.putExtra("NotifID", notifID);  
    PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.ic_launcher, "See activity!", System.currentTimeMillis());
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    notif.defaults |= Notification.DEFAULT_SOUND;
    CharSequence from = "Notification from me";
    CharSequence message = "See activity!";        
    notif.setLatestEventInfo(this, from, message, detailsIntent);
    nm.notify(notifID, notif);
    finish();
}

希望有人能帮助我:)

谢谢!

编辑:

感谢 Chirag_CID!我应该使用广播接收器而不是另一个活动。因此,DisplayNotification 现在扩展了广播接收器,而不是扩展 Activity。这是DisplayNotification的onReceive方法:

public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "DisplayNotification onReceive");
    int notifID = intent.getExtras().getInt("NotifID");
    Intent i = new Intent(context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    i.putExtra("NotifID", notifID);  
    PendingIntent detailsIntent = PendingIntent.getActivity(context, 0, i, 0);
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.ic_launcher, "See the quote of the day!", System.currentTimeMillis());
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    notif.defaults |= Notification.DEFAULT_SOUND;
    CharSequence from = "Notification";
    CharSequence message = "See the activity!";        
    notif.setLatestEventInfo(context, from, message, detailsIntent);
    nm.notify(notifID, notif);
}

【问题讨论】:

    标签: android android-intent notifications android-activity


    【解决方案1】:

    我把整个问题读了 2-3 遍..

    现在我想指出一些事情,

    您已创建Activity C,您将在其中创建通知,该通知将在其点击时调用 MainActivity

    但是,当您从 B 设置警报时,您正在传递 intent of Activity C,虽然您的 C 代码 没有 setContentView 方法,并且还有你有 在 onCreate() 的末尾写了 finish() 所以当通过 B 中的警报集调用 Activity C 时,它只是调用 Activity C 并设置 Notification ..但是正如你写的 finish() 它结束 Activity C 并显示堆栈中最新的 Activity B

    如果你想交叉检查.. 在Activity C的onCreate()、onPause()和onDestroy()中写入日志..你会看到Activity C在Alarm触发时被创建和销毁。

    解决方案: 如果您不想在主屏幕和警报触发器上显示任何活动,那么, 在活动 B 中,您为活动 C 编写了待处理的意图。您将需要它更改为调用广播接收器并使用 PendingIntent.getBroadcast 其中的 PendingIntent.getActivity 和您用 C 编写的代码通知您必须在 Receiver 中写入...因此,当警报触发时,您的任何活动都不会被调用。

    快乐编码:)

    【讨论】:

    • 感谢 Chirag_CID!我应该使用广播接收器而不是另一个活动。因此,DisplayNotification 现在扩展了广播接收器,而不是扩展 Activity。这是DisplayNotification的onReceive方法:
    猜你喜欢
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2015-01-29
    • 2016-07-07
    相关资源
    最近更新 更多