我有一个播放音乐的活动(Musicplayer.class),当曲目开始播放时会显示通知但是当我点击通知时它会将我带回主活动(应用程序的第一个活动) 如何修改代码以便点击通知将我带到音乐播放器活动。
代码取自android文档(见下文)
final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MusicPlayer.this)
// set values....
Intent resultIntent = new Intent(MusicPlayer.this, MusicPlayer.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MusicPlayer.this);
stackBuilder.addParentStack(MusicPlayer.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final int mId = 1;
mNotificationManager.notify(mId,mBuilder.build());
将代码更改为:
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pendingIntent);
现在活动重复(两个相同的活动正在运行,同一个音乐播放2次)