您好我已经能够为我的活动和用户显示通知 单击应用程序重新启动的通知。但是我只是希望它重新出现而不是重启。例如。它是一个Web应用程序,我希望它在用户选择通知时出现在前面。但是不刷新网页。我可以捕获这个意图还是我发错了意图?通常,如果我按下主页按钮并单击应用程序图标,应用程序就会出现并且不会刷新/重新启动。所以这就是我想要的行为。有什么想法吗?
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);
//2.Instantiate the Notification
int icon = R.drawable.notification_icon;
CharSequence tickerText = "My App"; // temp msg on status line
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
//3.Define the Notification's expanded message and Intent:
Context context = getApplicationContext();
CharSequence contentTitle = "Notification";
CharSequence contentText = "My app!"; // message to user
Intent notificationIntent = new Intent(this, HelloAndroid2.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
//4.Pass the Notification to the NotificationManager:
final int NOTIFICATION_ICON_ID = 1;
mNotificationManager.notify(NOTIFICATION_ICON_ID, notification);
答案 0 :(得分:25)
答案 1 :(得分:9)
如果您不得不将活动设置为“singleInstance”,请按如下方式设置通知的意图:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
答案 2 :(得分:4)
你可以尝试这个FLAG_ACTIVITY_REORDER_TO_FRONT(该文件准确描述了你想要的东西)
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
Intent notificationIntent = new Intent(this, HelloAndroid2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
答案 3 :(得分:0)
我建议为这种特殊情况设置标志Intent.FLAG_ACTIVITY_SINGLE_TOP,如果你使用android:launchMode =“singleInstance”它会影响你在应用程序中调用其他意图时的行为,例如使用facebook sdk,paypal sdk等..
这里的解释很简单: link
答案 4 :(得分:0)
我在这里还有另一种解决方案:
//Resume or restart the app (same as the launcher click)
val resultIntent = Intent(context, MyLauncherActivity::class.java)
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER)
resultIntent.setAction(Intent.ACTION_MAIN)
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(pendingIntent) //builder is the notificationBuilder
答案 5 :(得分:0)
如果有,请从.MainActivity活动部分的清单中删除它:
from PIL import Image
trophy_img = Image.open(r'C:\Users\Tallen\Desktop\Scripts\Pillow_Test\Trophy.jpg');
trophy_img.show()
这对于无法在Android设备(版本高于7)的三星设备(及其他设备)上恢复应用程序是致命的