0

내 앱을 종료 할 때 서비스를 시작하고 싶습니다.이전 서비스를 중지하려면 어떻게해야합니까?

코드는 다음과 같습니다

Intent i_service = new Intent(MainActivity.this,check.class); 
startService(i_service); 

을 그리고 난 내 응용 프로그램을 다시 열 때, 나는 내 응용 프로그램에서 때 종료를 시작 내 서비스를 중지합니다.

코드는 다음과 같습니다

Intent i_service = new Intent(MainActivity.this,check.class); 
stopService(i_service); 

이 그것을 할 올바른 방법은 무엇입니까?

이전 서비스를 어떻게 중지시킬 수 있습니까? 사전

+0

예 올바르게 수행하고 있습니다. –

+0

내 서비스가 이미 중지 된 경우 오류가 발생했습니다. –

+0

아니요. 오류가 발생하지 않습니다. 확인하고 실행 중이면 중지됩니다. –

답변

1
Check Your service is run or not and then stop ur service on oncreate() of ur activity as below, 



    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
     if (check.getName().equals(service.service.getClassName())) { 
      Intent i_service = new Intent(MainActivity.this,check.class); 
stopService(i_service); 
     } 
    } 
1

에서

덕분에 나는 당신이 올바른 방법으로 그 일을 생각합니다. 응용 프로그램이 다시 시작될 때 먼저 응용 프로그램이 실행 중인지 여부를 확인한 다음 응용 프로그램을 중지해야합니다.

위의 "MSS"에서 설명한 코드를 사용한 대답은 잘 설명합니다!