在我的应用程序中,如果发生特殊情况,我会用通知通知用户:
public void triggerNotification(String msg) {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent contentIntent = new Intent(this, ABC.class);
Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
notification.setLatestEventInfo(this, "ABC", msg, PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationCounter, notification);
notificationCounter++;
}
如果用户单击通知,就会调用onCreate()方法。但我希望调用应用程序中的特定方法,或者如果应用程序不在前台,则将其带回前台。
我知道有很多教程解释了如何处理通知,但我就是不能完全理解它们,也不能实现我想要的东西。
发布于 2010-07-31 23:57:33
要将你的应用程序带到前台,如果它已经在运行,你需要在你的意图上设置不同的标志:
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
对于运行特定的方法,您可以只传递额外的信息和意图,并在应用程序中解释它,以决定运行哪个方法。
发布于 2013-02-04 10:43:15
使用FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_SINGLE_TOP的建议只能部分解决这个问题。Android清单中的活动也应该应用这些设置,以便从主屏幕启动活动具有相同的行为。如果没有这些属性,可以启动活动的多个实例。
<activity android:name="foo"
android:clearTaskOnLaunch="true"
android:launchMode="singleTop"
android:label="@string/app_name">
发布于 2017-01-01 21:43:14
我发现,如果使用Intent contentIntent = new Intent(this, ABC.class);
,则无论设置了什么标志,它都会调用onCreate();
。
使用Intent contentIntent = getIntent();
跳过onCreate();
,它将移动到onStart();
https://stackoverflow.com/questions/3378193
复制相似问题