Java Android:如何避免点击通知调用onCreate()
在我的应用程序中,如果发生特殊情况,我会通知用户:Java Android:如何避免点击通知调用onCreate(),java,android,user-interface,notifications,Java,Android,User Interface,Notifications,在我的应用程序中,如果发生特殊情况,我会通知用户: public void triggerNotification(String msg) { notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent contentIntent = new Intent(this, ABC.class); Notification notif
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()方法。但我希望调用我的应用程序中的特定方法,或者如果应用程序不在前台,则将其带回前台
我知道有很多教程解释了如何处理通知,但我只是不完全理解它们,也无法实现我想要实现的功能。要让你的应用程序在已经运行的情况下进入前台,你需要设置不同的标志:
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
对于运行特定的方法,您可以将额外的信息与意图一起传递,并在应用程序中对其进行解释,以决定运行哪个方法。建议使用FLAG\u ACTIVITY\u CLEAR\u TOP和FLAG\u ACTIVITY\u SINGLE\u TOP只能部分解决问题。Android清单中的活动也应该应用这些设置,以便从主屏幕启动活动时具有相同的行为。如果没有这些属性,可以启动活动的多个实例
<activity android:name="foo"
android:clearTaskOnLaunch="true"
android:launchMode="singleTop"
android:label="@string/app_name">
我发现如果您使用
Intent contentIntent=newintent(这个,ABC.class)
this调用onCreate()代码>不考虑设置的标志
使用Intent contentIntent=getIntent()
跳过onCreate()代码>并移动到onStart()代码>是的,我在我的意图中添加了一些额外的内容。但是我在哪里可以提取这些额外的信息呢?按照您的版本,onCreate()不被调用。好的,我找到了答案:Activity#onNewIntent(Intent-Intent)被调用。对我来说,如果不添加这些行,stealthcopter提供的解决方案将无法工作。。。