希望我的问题彼此相关。我运行一个倒计时器服务,在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;
}
答案 0 :(得分:0)
如果您不想打开MainActivity
,那么您就不应该编写此代码:
Intent intent = new Intent(BroadcastService.this, MainActivity.class);
...
startActivity(intent);
打开MainActivity
正是这样做的。