我正在制作一個音樂應用程序,它在播放音頻時顯示通知。單擊時,此通知將通過意圖打開主 (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; }
3 回答

翻過高山走不出你
TA貢獻1875條經驗 獲得超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。

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
嘗試刪除 flag:Intent.FLAG_ACTIVITY_CLEAR_TOP
并添加下一個:Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
添加回答
舉報
0/150
提交
取消