【问题标题】:Notification always creates new activity instead of resuming previous activity通知总是创建新的活动,而不是恢复以前的活动
【发布时间】:2018-09-09 23:58:11
【问题描述】:

我正在制作一个音乐应用程序,它会在播放音频时显示通知。单击时,此通知会通过意图打开主 (UI) 活动。

虽然这应该是一个非常简单的过程,但由于某种原因,无论我做什么,主要活动总是在按下通知时被破坏。我已经尝试过 singleTop、singleTask(作为意图标志和清单值)、保存实例状态包、onNewIntent,基本上是我能找到的任何接近解决方案的东西。但活动总是被破坏。我只能通过getIntent获取意图。

主要活动:https://pastebin.com/32uuK33E

音频服务:https://pastebin.com/ShiUfBMc

清单:https://pastebin.com/kPp7RSYK


因为“指向pastebin的链接必须附有代码”

这是意图(我已经尝试了所有相关标志的组合,所以我真的怀疑这是问题所在)

// creates an intent which opens the application when the notification is pressed
        private PendingIntent getPendingIntent(Context context) {
            Intent intent = getIntent(this);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntent(intent);
            return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        }

        // returns an intent which resumes the Main Activity and passes various song information
        private Intent getIntent(Context context) {
            int duration = player != null ? player.getDuration() : 0;
            boolean playing = player != null && player.isPlaying();

            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO, getSongInfo());
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_POS, currentQueuePos);
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, duration);
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_IS_PLAYING, playing);
            return intent;
        }

这是我试图读取数据的地方

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.e(LOG_TAG, "flag got intent");
    String[] info = intent.getStringArrayExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO);
    int pos = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_POS, 0);
    int duration = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, 0);
    unpackageSongInfo(info);
    currentQueuePos = pos;
    seekBar.setMax(duration);
    setIntent(intent);
}

【问题讨论】:

标签: java android android-activity service notifications


【解决方案1】:

尝试删除标志:Intent.FLAG_ACTIVITY_CLEAR_TOP 并添加下一个:Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

【讨论】:

  • 使用这些标志尝试 PendingIntent.geyActivity()(来自上面的答案)
  • 那也没用。我已经解决了这个问题,需要调用 setSessionActivity (见我的回答)
【解决方案2】:

您可以使用launch modes 进行活动。

<activity android:name=".MainActivity"
          android:launchMode="singleTop"

singleTop:如果 Activity 的实例已经存在于目标任务的顶部,系统会通过调用其 onNewIntent() 方法将 Intent 路由到该实例,而不是创建 Activity 的新实例。

【讨论】:

  • “我已经尝试过 singleTop、singleTask(作为意图标志和清单值)”......我已经将 launchMode 设置为 singleTop 和 singleTask,但都没有工作。单击通知时,应用程序始终会被销毁并重新创建。
【解决方案3】:

我已经解决了这个问题。我需要在我的媒体会话中调用 setSessionActivity() 方法,如下所示:

Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, MediaPlayerContract.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSessionCompat.setSessionActivity(pendingIntent);

编辑:

您也可以通过调用来创建意图

setContentIntent(controller.getSessionActivity())

在调用 setSessionActivity 之后,控制器就是你的 MediaSessionController。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多