按下后退按钮时,Android音乐服务停止

时间:2014-03-25 19:32:37

标签: android service back-button

我正在处理在两个不同活动之间切换的android代码。有一项服务可以播放这两种活动的音乐。问题是,当我使用后退按钮时,服务(音乐)停止。我已经完成了关于这个主题的研究,我已经有了onResume和onRestart方法....我无法弄清楚下一步该做什么,任何帮助将不胜感激。活动和服务都列在下面。

MainActivity.java

public class MainActivity extends Activity {
MusicService myService;
private boolean isBound;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    isBound=false;
    playAudio();

  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}




public void onButtonClick(View v) {


    Intent intent = new Intent(this, Second_activity.class);

      startActivity(intent);
       onStop();

    ///Intent i= new Intent(this,EmpireShips.class);
   //// startActivity(i);
   //// onStop();
 }



 public void playAudio() {
     Intent objIntent = new Intent(this,  MusicService.class);
     if(!isBound)
     {
     bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
     isBound=true;

     startService(objIntent);
     }
     else
     {
         myService.plauseAudio();

         isBound=false;
         unbindService(myConnection);
     }

 }


 public void stopAudio(View view) {
     Intent objIntent = new Intent(this, MusicService.class);
     if(isBound)
     {
         isBound=false;
     unbindService(myConnection);   
     stopService(objIntent);

     }
     else
         stopService(objIntent);

 }

 private ServiceConnection myConnection = new ServiceConnection() {



     public void onServiceConnected(ComponentName className,
             IBinder service) {
         myService = ((MusicService.MyLocalBinder) service).getService();
         isBound = true;
     }

     public void onServiceDisconnected(ComponentName arg0) {
         isBound = false;
     }

    };

     @Override
     protected void onDestroy() {
         super.onDestroy();
         if (isBound) {
             // Disconnect from an application service. You will no longer
             // receive calls as the service is restarted, and the service is
             // now allowed to stop at any time.
             unbindService(myConnection);
             isBound = false;

         }
     }

     public void onStop(){
        super.onStop();
         if (isBound) {
              // Disconnect from an application service. You will no longer
              // receive calls as the service is restarted, and the service is
              // now allowed to stop at any time.
              unbindService(myConnection);
              isBound = false;
              myService.plauseAudio();

         }
     }

Second_activity.java

public class Second_activity extends Activity {
MusicService myService;
private boolean isBound;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    isBound=false;
    playAudio();



}

public void playAudio() {
    Intent objIntent = new Intent(this,  MusicService.class);
    if(!isBound)
    {
    bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
    isBound=true;

    startService(objIntent);
    }
    else
    {
        myService.plauseAudio();

        isBound=false;
        unbindService(myConnection);
    }

}


public void stopAudio(View view) {
    Intent objIntent = new Intent(this, MusicService.class);
    if(isBound)
    {
        isBound=false;
    unbindService(myConnection);   
    stopService(objIntent);

    }
    else
        stopService(objIntent);

}

private ServiceConnection myConnection = new ServiceConnection() {



    public void onServiceConnected(ComponentName className,
            IBinder service) {
        myService = ((MusicService.MyLocalBinder) service).getService();
        isBound = true;
    }

    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;
    }

   };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isBound) {
            // Disconnect from an application service. You will no longer
            // receive calls as the service is restarted, and the service is
            // now allowed to stop at any time.
            unbindService(myConnection);
            isBound = false;

        }
    }

    public void onStop(){
        super.onStop();
         if (isBound) {
             // Disconnect from an application service. You will no longer
             // receive calls as the service is restarted, and the service is
             // now allowed to stop at any time.
             unbindService(myConnection);
             isBound = false;
             myService.plauseAudio();

         }
    }


    @Override
    protected void onResume() {
        super.onResume();
        Intent objIntent = new Intent(this,  MusicService.class);
        if (!isBound) {
            // Disconnect from an application service. You will no longer
            // receive calls as the service is restarted, and the service is
            // now allowed to stop at any time.
         bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
            isBound=true;

         startService(objIntent);
        }
}



    public void onRestart(){
        super.onRestart();
         Intent objIntent = new Intent(this,  MusicService.class);
         if (!isBound) {
             // Disconnect from an application service. You will no longer
             // receive calls as the service is restarted, and the service is
             // now allowed to stop at any time.
             bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
                isBound=true;

         myService.play();
         }

    }








}



     @Override
     protected void onResume() {
         super.onResume();
         Intent objIntent = new Intent(this,  MusicService.class);
         if (!isBound) {
             // Disconnect from an application service. You will no longer
             // receive calls as the service is restarted, and the service is
             // now allowed to stop at any time.
             bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
                isBound=true;

             startService(objIntent);
         }
  }



     public void onRestart(){
        super.onRestart();
         Intent objIntent = new Intent(this,  MusicService.class);
         if (!isBound) {
              // Disconnect from an application service. You will no longer
              // receive calls as the service is restarted, and the service is
              // now allowed to stop at any time.
             bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
                isBound=true;

         myService.play();
         }

     }




}

MusicService.java

public class MusicService extends Service{

    private static final String LOGCAT = null;
    MediaPlayer AudioPlayer;

    public void onCreate(){
        super.onCreate();
        Log.d(LOGCAT, "Service Started!");
        AudioPlayer = MediaPlayer.create(this,R.raw.starwars_song);
    }

    public int onStartCommand(Intent intent, int flags, int startId){
        AudioPlayer.start();
        Log.d(LOGCAT, "Media Player started!");
        if(AudioPlayer.isLooping() != true){
            Log.d(LOGCAT, "Problem in Playing Audio");
        }
        return START_STICKY;
    }

    public void onStop(){
        AudioPlayer.stop();
        AudioPlayer.release();
    }
    public void onPause(){
        AudioPlayer.stop();
        AudioPlayer.release();
    }
    public void onDestroy(){
        AudioPlayer.stop();
        AudioPlayer.release();
    }
    public class MyLocalBinder extends Binder {
        MusicService getService() {
            return MusicService.this;
        }
    }
    private final IBinder myBinder = new MyLocalBinder();

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return myBinder;
    }
    public void plauseAudio() {
        if(AudioPlayer.isPlaying())
              AudioPlayer.pause(); 

    }

    public void play() {

              AudioPlayer.start(); 

    }
}

2 个答案:

答案 0 :(得分:0)

您实施的方式存在多个问题。

  1. 您必须在活动创建时启动服务(如果服务已在运行,将调用Service.onStartCommand。仅在非空时创建MediaPlayer实例)。
  2. 在OnCreate中启动服务后绑定到服务以建立ServiceConnection
  3. 保留服务实例直到活动被销毁
  4. 如果需要,您可以暂停onPause中的媒体,并在onResume时重新开始。
  5. 在onDestroy活动上取消绑定服务。
  6. 退出应用程序时,如果需要,请停止服务,否则服务将运行。当应用程序从堆栈中完全删除时,服务也将停止。通过使用通知将服务作为Foreground运行,可以避免服务被杀死。

答案 1 :(得分:0)

只需删除MusicService中的onDestroy方法,音乐就会继续,但您的服务会被破坏。