2017-11-16 26 views
0

내 응용 프로그램은 배경 음악 서비스를 사용합니다. 내 앱을 종료하는 버튼이 있지만 앱과 서비스를 종료 할 수있는 항목이 없습니다. 서비스를 내 활동에 바인딩합니다.서비스 중지 및 응용 프로그램 닫기

unbindService(serviceConnection); 
myService().stopSelf(); 
stopService(new Intent(this, MediaPlayer.class)); 

을 절대적으로 아무것도 작동! :

나는 시도 서비스는 계속됩니다.

내 서비스를 파기하려면 어떻게해야합니까? 내 응용 프로그램을 닫으려면 어떻게해야합니까 ??

텍사스

편집 : 나는에서 onCreate 방법

Intent intent = new Intent(this, serviceClass); 
bindService(intent, serviceConnection, BIND_AUTO_CREATE); 

에 그리고 MediaPlayer를 클래스

public class LocalBinder extends Binder { 
     public MediaPlayer getService() { 
      return MediaPlayer.this; 
     } 
    } 
public IBinder onBind(Intent intent) { 
    Log.i(TAG, "service bound"); 
    init(); 
    return mBinder; 
} 

그리고 ... 하지만 그나마이를 사용

내가 정말로 서비스를 시작해야하는지 안다. 서비스 바인딩 이미 지금에게

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_NOT_STICKY; 
} 

을 시작 나는이

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

들의 OnDestroy 방법은 내가 서비스 바인딩을 해제 할 경우에만 작동했다! 모든 이 나던 작업 :

그래서
 getService().stopSelf(); 
     this.stopService(new Intent(this, MediaPlayer.class)); 

, 어떻게 서비스를 중지 할 수 있으며 어떻게 앱을 닫을 수 있습니다?

+0

서비스를 어떻게 시작합니까? stopSelf()는 해당 서비스에서 호출 될 때만 작동합니다. –

+0

"서비스 중지"란 무엇을 의미합니까? 그 중 몇몇은 서비스를 중단 할 것입니다. 그러나 서비스를 중지해도 서비스가 제대로 중지되지 않으면 onDestroy를 처리하지 않으면 음악이 중단되지 않습니다. –

+0

질문이 명확하지 않습니다 ... 일부 코드 및 설명을 추가하십시오. – rafsanahmad007

답변

0

이것은 내 앱에서하는 일입니다. 앱을 닫으면 해당 활동의 onDestroy() 메소드가 호출됩니다.

private ServiceConnection musicServiceConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     MusicService.LocalBinder binder = (MusicService.LocalBinder) service; 
     musicService = binder.getService(); 
     musicService.setCallbacks(MainActivity.this); 
     musicServiceBound = true; 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     Log.i(TAG, "MusicService service disconnected (unbinded)"); 
     musicServiceBound = false; 
    } 
}; 


@Override 
protected void onStart() { 
    super.onStart(); 
    Intent intent1 = new Intent(this, MusicService.class); 
    bindService(intent1, musicServiceConnection, Context.BIND_AUTO_CREATE); 
} 


@Override 
protected void onDestroy() { 
    super.onDestroy() 
    if(musicServiceBound){ 
     musicService.stopSelf(); 
     unbindService(musicServiceConnection); 
    } 
} 

당신은 당신이 ()를 사용하여 다른 서비스를 만드는 myService()을 썼다. 프로그래밍 방식으로 앱을 닫으려면 question을 참조하십시오.

+0

서비스 온 디스트 로이입니까? –

+1

활동 onDestroy –

+0

어딘가에 서비스를 시작 하시겠습니까? 아니면 그냥 바인딩합니까? –