如何通过通知将 Android 现有活动置于前台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3356095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bring Android existing activity to front via notification
提问by user404012
I have one Android application, when it runs a service, I want to show the notification on the status bar. Then the user could navigate to other application by pressing HOME key. However when I try to bring the previous running application back to Front via notification icon, there is some problem with the existing activity. Even I declare it as "Single Top" mode (I want to run the existing activity since there is an associated service running) , somehow that activity's OnDestroy has been called before OnResume. Here is my code of creating the notification object. Could you please point me what wrong it is. Thanks.
我有一个 Android 应用程序,当它运行一项服务时,我想在状态栏上显示通知。然后用户可以通过按 HOME 键导航到其他应用程序。但是,当我尝试通过通知图标将先前运行的应用程序带回 Front 时,现有活动存在一些问题。即使我将其声明为“单顶”模式(我想运行现有活动,因为有相关的服务正在运行),不知何故,该活动的 OnDestroy 已在 OnResume 之前被调用。这是我创建通知对象的代码。你能指出我这是什么问题吗?谢谢。
private void showNotification ()
{
Intent toLaunch = new Intent(getApplicationContext(),
MySingleTopActivity.class);
PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(),
getText(R.string.GPS_service_name), text, intentBack);
....
}
回答by santhosh
private void showNotification() {
Intent toLaunch = new Intent(getApplicationContext(), MySingleTopActivity.class);
//add these two lines will solve your issue
toLaunch.setAction("android.intent.action.MAIN");
toLaunch.addCategory("android.intent.category.LAUNCHER");
PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0, toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack);
...
}
回答by Sharique Abdullah
I'd recommend you do this,
我建议你这样做,
private void showNotification ()
{
Intent toLaunch = new Intent(getApplicationContext(), DummyActivity.class);
// You'd need this line only if you had shown the notification from a Service
toLaunch.setAction("android.intent.action.MAIN");
PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(), 0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);
....
}
The DummyActivity should simply be an activity that always finishes itself in the oncreate event.
DummyActivity 应该只是一个总是在 oncreate 事件中完成自己的活动。
In the manifest file, add these lines
在清单文件中,添加这些行
<activity class=".DummyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
I hope this helps ...
我希望这有帮助 ...
回答by
Another way to launch your package intent.
启动包意图的另一种方式。
private void NotificationwithLaucherSelfPackage(Context context , int notification_id){
Notification noti = new Notification.Builder(context)
.setContentTitle("Your Title")
.setContentText("Your Text")
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentIntent(PendingIntent.getActivity( context ,notification_id , getLauncherIntent(context) , PendingIntent.FLAG_UPDATE_CURRENT))
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
}
private Intent getLauncherIntent(Context context){
return context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
}
回答by Cheryl Simon
When an Activity is in the background, Android may kill the activity at any time to free resources. See the Activitydocumentation for complete details on the lifecycle.
当 Activity 在后台时,Android 可能会随时杀死该 Activity 以释放资源。有关生命周期的完整详细信息,请参阅活动文档。
Your activity needs to be prepared to save its state in the onPause or onSaveInstanceState methods. The document referenced above has additional details on saving persistant state.
您的 Activity 需要准备好将其状态保存在 onPause 或 onSaveInstanceState 方法中。上面引用的文档包含有关保存持久状态的其他详细信息。