Android:如何避免点击通知调用 onCreate()

发布于 2024-09-12 10:30:44 字数 818 浏览 5 评论 0原文

在我的应用程序中,如果发生特殊情况,我会通过通知通知用户:

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() 方法。但我希望调用我的应用程序中的特定方法,或者如果应用程序不在前台,则将其带回前台。

我知道有很多教程解释如何处理通知,但我只是不完全理解它们,并且无法实现我想要的东西。

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++;
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿透光 2024-09-19 10:30:44

如果您的应用程序已经在运行,则要将其带到前台,您需要为您的意图设置不同的标志:

contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

要运行特定的方法,您只需传递额外的信息和意图,并在您的应用程序中解释它来决定运行哪个方法。

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.

牵强ㄟ 2024-09-19 10:30:44

使用 FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_SINGLE_TOP 的建议只能部分解决问题。 Android 清单中的 Activity 也应应用这些设置,以便从主屏幕启动 Activity 具有相同的行为。如果没有这些属性,则可以启动活动的多个实例。

<activity android:name="foo"
          android:clearTaskOnLaunch="true"
          android:launchMode="singleTop"
          android:label="@string/app_name">

The recommendation to use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP only partially solves the problem. The activity in the Android manifest should also have these settings applied so that launching the activity from the home screen has the same behavior. Without these properties multiple instances of the activity can be launched.

<activity android:name="foo"
          android:clearTaskOnLaunch="true"
          android:launchMode="singleTop"
          android:label="@string/app_name">
夏雨凉 2024-09-19 10:30:44

我发现,如果您使用 Intent contentIntent = new Intent(this, ABC.class); ,则无论设置的标志如何,都会调用 onCreate();

使用 Intent contentIntent = getIntent(); 跳过 onCreate(); 并移至 onStart();

I've discovered that if you use Intent contentIntent = new Intent(this, ABC.class); this calls onCreate(); regardless of the flags set.

Use Intent contentIntent = getIntent(); to skip onCreate(); and that moves to onStart();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文