I'm making a music app which displays a notification while audio is playing. When clicked, this notification opens the main (UI) activity through an intent.
While this should be a pretty simple process, for some reason no matter what I do the main activity is always destroyed when the notification is pressed. I've tried singleTop, singleTask (both as intent flags and manifest values), saving instance state bundles, onNewIntent, basically anything close to a solution I can find. But the activity is ALWAYS destroyed. I can only get the intent through getIntent.
Main Activity: https://pastebin.com/32uuK33E
Audio Service: https://pastebin.com/ShiUfBMc
Manifest: https://pastebin.com/kPp7RSYK
since "links to pastebin must be accompanied by code"
here's the intent (i've tried every combination of relevant flags so I really doubt that's the issue)
// 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;
}
and here's where i'm trying to read the data
@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);
}
尝试删除标志: Intent.FLAG_ACTIVITY_CLEAR_TOP
并添加下一个标志: Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
You can use launch modes for an activity.
<activity android:name=".MainActivity"
android:launchMode="singleTop"
singleTop: If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
I've solved the problem. I needed to call the setSessionActivity() method on my media session, like so:
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, MediaPlayerContract.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSessionCompat.setSessionActivity(pendingIntent);
EDIT:
you can also create an intent by simply calling
setContentIntent(controller.getSessionActivity())
after calling setSessionActivity, where controller is your MediaSessionController.
In my case the problem has been caused by TaskStackBuilder
. Solved by creating a pending intent via PendingIntent.getActivity()
.
So the way to go is:
1. Set a proper launchMode
for activity (Single top eg)
2. call MediaSession.setSessionActivity(pendingIntent)
before build()
in onCreate()
of your service.
3. Create a pending intent using PendingIntent.getActivity()
instead of TaskStackBuilder
. If this one did the trick, seek the problem deeper in TaskStackBuilder
(if you really need to use it)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.