如何通过通知带来的Andr​​oid现有的活动前 [英] How to bring Android existing activity to front via notification

查看:112
本文介绍了如何通过通知带来的Andr​​oid现有的活动前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,当它运行的服务,我想显示在状态栏上的通知。然后,用户可以浏览到其他应用程序通过pressing HOME键。然而,当我试图把previous运行的应用程序返回到通过通知图标前,有一些问题,与现有的活动。即使我宣布为单顶模式(我想要运行现有的活动,因为有一个相关的服务运行),不知何故该活动的的OnDestroy已经OnResume之前调用。这是我的$ C $创建通知对象的温度。能否请你点我它是什么错。谢谢。

 私人无效showNotification()
{

  意图toLaunch =新的意图(getApplicationContext()
                                          MySingleTopActivity.class);

  PendingIntent intentBack = PendingIntent.getActivity(getApplicationContext(),0,toLaunch,PendingIntent.FLAG_UPDATE_CURRENT);

  notification.setLatestEventInfo(getApplicationContext(),
         的getText(R.string.GPS_service_name),文本,intentBack);
....
}
 

解决方案

 私人无效showNotification(){
    意图toLaunch =新的意图(getApplicationContext(),MySingleTopActivity.class);

    //添加这两条线将解决您的问题
    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),文本,intentBack);
    ...
}
 

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.

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);
....
}

解决方案

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);
    ...
}

这篇关于如何通过通知带来的Andr​​oid现有的活动前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆