Skip to content Skip to sidebar Skip to footer

Android, How To Stop Service Playing Music When Pausing App ,

I am building a simple android application and it has two Activities and a IntentService. My IntentService plays music over every activity(thats what I want) but if I leave the ap

Solution 1:

For controlling BackgroundMusic Service from Activity you will need to use custom BroadcastReceiver to communicate with service when application is going in onpause state.

Register BroadcastReceiver In BackgroundMusic :

publicclassMusicServiceBroadCastextendsBroadcastReceiver {

     @OverridepublicvoidonReceive(Context context, Intent arg1) {

                    if(<Match for action>){
                      if Action is for pause then call pause for MediaPlayer
                    }else{
                      if Action for Play ...
                    }
        }

    } 
 @OverrideprotectedvoidonHandleIntent(Intent intent) {
    //... Register BroadcastReceiver
    registerReceiver(newMusicServiceBroadCast(), newIntentFilter(
     "com.xx.PAUSE_MUSIC_ACTION"));
    HN.post(newPlayMusic()); 

 }

Send BroadcastReceiver from Activity onPause :

Intentintent=newIntent();
 intent.setAction("com.xx.PAUSE_MUSIC_ACTION");
 sendBroadcast(intent);

Post a Comment for "Android, How To Stop Service Playing Music When Pausing App ,"