Android,暂停应用程序时如何停止服务播放音乐,

时间:2014-02-07 03:31:01

标签: android service

我正在构建一个简单的Android应用程序,它有两个Activity和一个IntentService。我的IntentService在每个活动上播放音乐(这就是我想要的)但是如果我离开应用程序音乐仍在播放(例如:如果我按下主页按钮它将带我到桌面,将我的活动置于暂停状态但音乐从那个应用程序仍在播放)。任何帮助将不胜感激....

下面的代码

主要活动

public class MainActivity extends Activity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent serviceIntent= new Intent(this, BackgroundMusic.class);
    startService(serviceIntent);



}




public void ShipPick(View view){

    Intent activityIntent= new Intent(this, ShipChoiceActivity.class);
    startActivity(activityIntent);

}


}

背景音乐服务

public class BackgroundMusic extends IntentService {

MediaPlayer mp;

 public BackgroundMusic() {
      super("BackgroundMusic");
  }


 Handler HN = new Handler(); 


 private class PlayMusic implements Runnable {



       public void PLayMusic(){

       }

       public void run(){
           mp = MediaPlayer.create(getApplicationContext(), R.raw.musicfile);
        mp.start();
       }
 }

 @Override
 protected void onHandleIntent(Intent intent) {

    HN.post(new PlayMusic()); 

 }

 public void onPause() {

        mp.pause();
        }

 public void onResume() {

     mp.start();
     }

 protected void onStop() {

     mp.stop();
     mp = null;
     }


 }

第二项活动

public class ShipChoiceActivity extends Activity {



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ship_choice);


}

}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.starwars"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <service android:name=".BackgroundMusic" />

    <activity
        android:name="com.example.ship.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/app_name" android:name="ShipChoiceActivity"/>
</application>

</manifest>

1 个答案:

答案 0 :(得分:1)

要从Activity中控制BackgroundMusic服务,当应用程序进入暂停状态时,您需要使用自定义BroadcastReceiver与服务进行通信。

在BackgroundMusic中注册BroadcastReceiver:

    public class MusicServiceBroadCast extends BroadcastReceiver {

     @Override
       public void onReceive(Context context, Intent arg1) {

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

    } 
 @Override
 protected void onHandleIntent(Intent intent) {
    //... Register BroadcastReceiver
    registerReceiver(new MusicServiceBroadCast(), new IntentFilter(
     "com.xx.PAUSE_MUSIC_ACTION"));
    HN.post(new PlayMusic()); 

 }

从活动onPause发送BroadcastReceiver:

 Intent intent = new Intent();
 intent.setAction("com.xx.PAUSE_MUSIC_ACTION");
 sendBroadcast(intent);