Android:如何避免点击通知调用 onCreate() [英] Android: How to avoid that clicking on a Notification calls onCreate()
问题描述
在我的应用程序中,如果发生特殊情况,我会通过通知通知用户:
In my application I notify the user with notifications, if something special happens:
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() 方法.但我希望我的应用程序中的特定方法被调用,或者如果应用程序不在前台,则将其带回前台.
If the user clicks on the Notification, the onCreate() method is called. But I want that a specific method in my app is called, or if the app is not in the foreground, that it is brought back to the foreground.
我知道有很多教程解释了如何处理通知,但我只是不完全理解它们,并且无法实现我想要的东西.
I know there are lots of tutorials that explain how to handle notifications, but I just don't understand them completely and wasn't ever able to implement the things like I'd like to.
推荐答案
要将您的应用程序置于前台,如果它已经在运行,您需要在您的意图上设置不同的标志:
To bring your app to the foreground if it is running already you need to set different flags on your intent:
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
为了运行一个特定的方法,你可以只传递额外的信息和意图,并在你的应用程序中解释它来决定运行哪个方法.
For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.
这篇关于Android:如何避免点击通知调用 onCreate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!