gpt4 book ai didi

Android:如何从通知中恢复应用程序?

转载 作者:可可西里 更新时间:2023-11-01 19:06:07 24 4
gpt4 key购买 nike

我正在尝试将我的通知编程为恢复我的应用程序,而不是简单地启动我的应用程序的新实例......我基本上是在寻找它来做与长按主页按钮和应用程序从那里恢复。

这是我目前正在做的事情:

void notifyme(String string){

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);

int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = string + " Program Running..."; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = *********; // expanded message title
CharSequence contentText = string + " Program Running...";//expanded msg text

Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);

// the next two lines initialize the Notification, using the configurations
// above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}

我猜测新的 Intent 行是问题所在...任何帮助将不胜感激!

最佳答案

你需要设置你的标志

 notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

此外,如果您永远不想重复 Activity ,请在 list 中为其指定此属性

android:launchMode="singleTask"

关于Android:如何从通知中恢复应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com