主页按钮服务背景音乐



我有一个正在服务中的背景音乐,它工作正常,但是当我按下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;
}

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

顺便问一下,System.exit(0);在你的安卓代码中做什么?

因为当您单击HOME_BUTTON时,该服务不会调用 onDestory()。

您可以为HOME_BUTTON注册一个广播接收器,如下所示:

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));

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

最新更新