Saturday, 24 August 2013

Can't fix MediaController.show() exception

Can't fix MediaController.show() exception

I have an audio file playing in a foreground Service using MediaPlayer.
When a user taps the notification associated with the foreground Service,
I launch an Activity using the Intent like so:
Android 3.0+
Intent audioPlayIntent = new Intent(context, AudioPlayActivity.class);
audioPlayIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
audioPlayIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
audioPlayIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
audioPlayIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Android 2.1 - 3.0
Intent audioPlayIntent = new Intent(context, AudioPlayActivity.class);
audioPlayIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
audioPlayIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
audioPlayIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
audioPlayIntent, 0);
This Activity then binds to the service to show a MediaController to the
user.
Here is the binding code in the Service:
public class AudioPlayerServiceBinder extends Binder{
public AudioPlayerService getAudioService(){
return AudioPlayerService.this; //this class is declared in
AudioPlayerService.java, so it has access to the Service instance.
}
}
..and in the Activity's onStart I have a call to this method:
private void bindAudioService()
{
mediaController = new MediaController(this);
Intent i = new Intent(this, AudioPlayerService.class);
serviceConnection = new AudioServiceConnection();
bindService(i, serviceConnection, 0);
}
I'm getting a lot of reports of exceptions from the Android Developer
Console that report an error here on the mediaController.show() line:
private class AudioServiceConnection implements ServiceConnection{
AudioPlayerServiceBinder audioServiceBinder;
@Override
public void onServiceConnected(ComponentName name, IBinder
serviceBinder)
{
serviceConnected = true;
Log.i(TAG, "Connected to audio player service.");
AudioPlayActivity.this.audioService =
((AudioPlayerServiceBinder)serviceBinder).getAudioService();
audioServiceBinder = ((AudioPlayerServiceBinder) serviceBinder);
handler.post(new Runnable(){
@Override
public void run()
{
mediaController.setMediaPlayer(AudioPlayActivity.this);
mediaController.setAnchorView(AudioPlayActivity.this.findViewById(R.id.audioplay_container));
mediaController.setEnabled(true);
mediaController.show(5000); // THIS CAUSES AN
EXCEPTION ON SOME DEVICES, BUT NOT MINE
}
});
}
The exception being:
android.view.WindowManager$BadTokenException: Unable to add window --
token null is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:448)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
at android.view.Window$LocalWindowManager.addView(Window.java:526)
at android.widget.MediaController.show(MediaController.java:305)
at
com.jukaku.masjidnowlib.AudioPlayActivity$AudioServiceConnection$1.run(AudioPlayActivity.java:293)
The reason I put it in a Handler.post is because I was trying to fix this
issue, and thought that maybe the onServiceConnected is running in a
different thread than the UI thread and so couldn't touch the UI. This has
not stopped the exceptions, however.
I used to be able to recreate the same exception by:
Clicking the notification to open the Activity >> pressing back to close
the activity (and remove it from the Activity stack, since it has
FLAG_NO_HISTORY set) >> Clicking the notification again.
Once I put the mediaController.show() in the Handler, I would no longer
get the exception and I cannot recreate them. However, like I said, the
exceptions have not stopped being reported.
Any help would really be appreciated! I've gone through 10 releases trying
to fix this issue but still can't nail it down. It's especially hard
because I can't recreate it!

No comments:

Post a Comment