首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓:如何避免点击通知调用onCreate()

安卓:如何避免点击通知调用onCreate()
EN

Stack Overflow用户
提问于 2010-07-31 21:00:43
回答 3查看 9.3K关注 0票数 18

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

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

我知道有很多教程解释了如何处理通知,但我就是不能完全理解它们,也不能实现我想要的东西。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-31 23:57:33

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

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

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

票数 17
EN

Stack Overflow用户

发布于 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">
票数 8
EN

Stack Overflow用户

发布于 2017-01-01 21:43:14

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

使用Intent contentIntent = getIntent();跳过onCreate();,它将移动到onStart();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3378193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档