Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

background - Android Notification restarts app but want to resume

Hi I have a been able to get a notification displayed for my activity, and when the user clicks the notification the app restarts. However I just want it to reappear not restart. eg. it is a web app and I want it to come to the front when the user selects the notification..but not refresh the web page. Can I trap this intent or am I sending the wrong intent? Normally if I press the home button and click on the app icon the app comes to the fore and doesn't refresh/restart. So this is the behaviour I want. Any ideas ?

 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I put this in manifest

android:launchMode="singleInstance"

This fixed the problem for me. Also this answer which I have not tried which allude to other things of interest.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...