中断活动和多个活动问题

时间:2017-08-08 20:25:39

标签: android android-intent android-activity

希望我的问题彼此相关。我运行一个倒计时器服务,在onfinish方法中触发一个意图。完成后,它会自动打开/启动我不希望它执行的应用程序。为什么会这样?我喜欢它只是显示通知。此外,当我点击通知而不是仅恢复活动时,它会创建另一个。我认为由于意图问题彼此相关。这是代码

来自onFinish()的

不是来自通知

Intent intent = new Intent(BroadcastService.this, MainActivity.class);


            intent.putExtra("id1",id1);

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);



          startActivity(intent);

这是通知的代码

NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
    notificBuilder.setContentTitle("Loot");
    notificBuilder.setContentText("Claim your Gold now!");

    notificBuilder.setSmallIcon(R.drawable.gold2);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificBuilder.setSound(soundUri);
    notificBuilder.setAutoCancel(true);

    Intent backhome = new Intent(this, MainActivity.class);
    backhome.putExtra("id2",id2);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
    taskStackBuilder.addParentStack(MainActivity.class);
    taskStackBuilder.addNextIntent(backhome);
    PendingIntent pd = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


    notificBuilder.setContentIntent(pd);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notifID, notificBuilder.build());

    isNoteActive = true;

}

1 个答案:

答案 0 :(得分:0)

如果您不想打开MainActivity,那么您就不应该编写此代码:

Intent intent = new Intent(BroadcastService.this, MainActivity.class);
...
startActivity(intent);

打开MainActivity正是这样做的。