Stopping Background Service Music

旧街凉风 提交于 2019-12-12 23:32:14

问题


package com.falling.inairproandmark;



import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

    return null;
}
@Override
public void onCreate() {
    super.onCreate();


    player = MediaPlayer.create(this, R.raw.bgmusic);

    player.setVolume(100,100);

}
public int onStartCommand(Intent intent, int flags, int startId) {


    player.start();

    return 1;
}

public void onStart(Intent intent, int startId) {
    // TODO



}
public IBinder onUnBind(Intent arg0) {
    // TODO Auto-generated method stub

    return null;
}

public void onStop() {

}
public void onPause() {

}
@Override
public void onDestroy() {

    player.stop();
    player.release();
}

@Override
public void onLowMemory() {

    }
}

I don't understand coding much... But what I'm trying to do is play the music thru-out the activities.

Let's say I have 10 activities... I want the music to play while i go through them and when I get a call or exit the application by pressing Home key... Music stops or at least pauses... how do i do that?

Thanks

Wahid


回答1:


You need to launch separate service (different intent filter in the Manifest for the same serivce) in onStartCommand() you'll check the action of the intent i.e if the action has the same value as the one you specified for intent action in the manifest file and if the action matches the intent filter action name then just stop the service.

Example from one of my projects:

In the manifest file:

<service android:name=".MyPlayerService" android:permission="android.permission.MODIFY_AUDIO_SETTINGS">        
      <intent-filter >
        .... some other filters
        <action android:name="com.my.player.EXIT"/>
        ....
        </intent-filter>   
      </service>

In the onStartCommand(): Here we can see the need of specifying action name, which is used to distinguish numerous actions within the same service.

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i("ON START COMMAND", "Huston, we have a lift off!");

    if(intent.getAction().equals("com.my.player.EXIT")
             { // means that you have envoked action that will has to stop the service
                 MyPlayerService.this.stopSelf();   // See the note below
             }else if(//some other actions that has to be done on the service)

}

Note: Note that here you can simply stop the MediaPlayer from playing or pause it using .stop() or .pause(),or just terminate the service as I provided above.

Now back to the activity. Catching Home button is not good idea. But this you can do in the onDestroy() method, where the activity is not in the stack i.e just before it is destroyed. Here you just launch intent that will signal the service to stop working.

Example:

Intent exit = new Intent("com.my.player.EXIT"); //intent filter has to be specified with the action name for the intent
this.startService(exit);

Read more on Android Dev about stopSelf() If you are trying this approach starting the MediaPlayer good practice will be to make the playing has its own action name in the intent filter, and do the same checking in the onStartCommand()



来源:https://stackoverflow.com/questions/7620758/stopping-background-service-music

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!