0

我有一个播放音乐的活动(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 次)

4

0 回答 0