主页按钮服务背景音乐

时间:2014-12-02 14:40:10

标签: android audio android-service android-homebutton

我有一个正在服务的背景音乐,它工作正常但是当我按下HOME按钮时音乐不会停止。帮助我。

服务:

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.haha);
    player.setLooping(true); // Set looping
    player.setVolume(100,100);

}
public int onStartCommand(Intent intent, int flags, int startId) {
    player.start();
    return 1;
}

public void onStart(Intent intent, int startId) {
    // TO DO
}
public IBinder onUnBind(Intent arg0) {
    // TO DO Auto-generated method
    return null;
}

public void onStop() {

}
public void onPause() {

}
@Override
public void onDestroy() {
    player.stop();
    player.release();
}

@Override
public void onLowMemory() {

}

这是我的活动,其目的是:

public class MainActivity extends Activity implements OnClickListener {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   Intent svc=new Intent(this, BackgroundSoundService.class);//This is the intent
    startService(svc);  

    View adventuretime1 = this.findViewById(R.id.button1);
    adventuretime1.setOnClickListener(this);

    View advanced2 = this.findViewById(R.id.button2);
    advanced2.setOnClickListener(this);


}

            @Override
            public void onBackPressed(){

              new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit")
              .setMessage("Are you sure you want to exit?")
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                       @Override
                                       public void onClick(DialogInterface dialog, int which) {
                                          Intent intent = new Intent(Intent.ACTION_MAIN);
                                          intent.addCategory(Intent.CATEGORY_HOME);
                                          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                          startActivity(intent);
                                          System.exit(0);
                                       }
             }).setNegativeButton("No", null).show();
            }           


public void onClick(View v) {

    switch(v.getId()){
    case R.id.button1:
        Intent button1 = new Intent(this, AdventureTime.class);
        startActivity(button1);
        break;

    case R.id.button2:
        Intent button2 = new Intent(this, AdvancedKids.class);
        startActivity(button2);
        break;

}

3 个答案:

答案 0 :(得分:0)

这是一项服务和服务应该具有更长的UI活动或片段的生命周期。从您的代码中我看不到您在应用程序中处理HOME按钮并停止服务或类似情况的情况。你可能应该这样做。它只是另一个H / W按钮,它触发你可以背负的事件。

顺便问一下System.exit(0);你的android代码做了什么?

答案 1 :(得分:0)

因为当您点击 HOME_BUTTON 时,服务将不会调用 onDestory()

您可以为 HOME_BUTTON 注册broadcastReceiver,如下所示:

context.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //destory the service if you want
        }
    }, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

答案 2 :(得分:0)

如果您只想停止服务,则可以调用this方法。