且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过通知带来的Andr​​oid现有的活动前

更新时间:2022-10-17 11:54:31

 私人无效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);
    ...
}