【发布时间】:2012-10-12 14:17:56
【问题描述】:
我有一个定义为 singleTop 的 Activity,因此只有一个实例存在。
<activity
android:name=".MyMainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:launchMode="singleTop" android:multiprocess="true">
我用一些数据设置了一个通知意图,并将其包含在一个 PendingIntent 中,并将其发送到通知管理器。
Intent notificationIntent = new Intent(context, MyMainActivity.class);
notificationIntent.setAction(MyMainActivity.CustomInternalMessageAction);
notificationIntent.putExtra(MyMainActivity.ReceiveTestMessage, rawMessageText);
...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotificationManager.notify(number, notification);
如果 Activity 没有运行,Intent 会按预期通过 onCreate() 启动 Activity。
如果活动正在运行,但处于停止状态/不在前台(如单击主页按钮时),并且单击了通知,则按预期调用我的活动的 onNewIntent()。
但是,如果 Activity 在前台时已被调用,则不会调用 onNewIntent()。
如何设置此通知,以便在我的 singleTop Activity 处于暂停/停止状态时发送意图(并且在我提到的其他情况下仍然有效)。我想在我的 notificationIntent 对象上设置一个标志,但是对于 singleTop 活动的情况,标志的功能并不是很清楚。
【问题讨论】:
-
在这种情况下是否会调用其他任何
on方法? -
不,不是基于我的日志记录。 onCreate()、销毁、恢复、暂停、onSaveInst、onRestoreInst
-
这似乎只是某些设备上的问题。我正在处理类似的情况,我有一台运行良好的设备,另一台没有收到新的 Intent。在我的意图上设置
CLEAR_TOP标志修复了它在有问题的设备上。
标签: android android-intent notifications