如何通过通知将已有的Android活动置于前台

14

我有一个Android应用程序,在运行服务时,我想在状态栏上显示通知。然后用户可以通过按HOME键导航到其他应用程序。但是当我尝试通过通知图标将先前运行的应用程序带回前台时,现有活动存在问题。即使我将其声明为“Single Top”模式(因为存在相关服务),某种方式该活动的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);
....
}

可能的重复:https://dev59.com/sW855IYBdhLWcg3w_5cQ,https://dev59.com/Sm035IYBdhLWcg3wTuJu - Philipp
4个回答

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

通知.setLatestEventInfo(getApplicationContext(), getText(R.string.GPS_service_name), text, intentBack); 我认为text应该改为toLaunch。 - Siddhesh
请在声明PendingIntent时使用以下代码:PendingIntent contentIntent = PendingIntent.getActivity(this, 0, toLaunch, 0);,并在此基础上将+1。 - Calin

8
我建议您这样做:
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);

.... 
}

DummyActivity应该只是一个在oncreate事件中总是结束自身的活动。

在清单文件中添加以下行

<activity class=".DummyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

I hope this helps ...


这是我在Android 2.2+中唯一有效的解决方案。谢谢。 - Darcy
@Sharique Abdullah 这是一个非常聪明的解决方法,谢谢 :) - Lev
看起来有些复杂,但也使得在用户点击通知时记录分析事件变得容易。 - kenyee
@kenyee 是的,但如果您想在通知点击上放置一些逻辑和自定义行为,它会给予一定的自由...对我来说这是值得的。 - Sharique Abdullah

0

启动您的包意图的另一种方法。

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

-2

当一个活动在后台运行时,安卓可能会在任何时候终止该活动以释放资源。请参阅Activity文档以获取有关生命周期的完整详细信息。

你的活动需要准备好在onPause或onSaveInstanceState方法中保存其状态。上述引用文档中还有关于保存持久化状态的额外详细信息。


1
我认为这个活动不应该被称为“onDestroy”。只有当它进入后台时才会调用“onPause”,当它回到前端时才会调用“onResume”。如果我只是点击应用程序图标重新启动应用程序,那么没有问题,只会调用“OnResume”。但是,如果我尝试点击通知图标,则活动会“onDestroy”->“OnResume”,这会导致问题。 - user404012
当应用进入后台时,onPause方法会被调用。但是,正如参考文档中所述,如果Android需要资源,它可能随时选择终止Activity,因此onDestroy方法也会被调用。您不能依赖于onDestroy方法不会被调用的事实,因此您需要通过保存状态来正确处理它。 - Cheryl Simon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接