를 작동하지 않는 나는 다음을 않는 IntentService 있습니다. println()을 서버로 보내진 HTTP 요청으로 대체하려고합니다. 그러나 지금은 이것을 테스트로 사용하고 있습니다. 나는 같은 startService()를 호출 :stopService()
내 사용자 지정 startService 방법이 수행@Override
public void onPause() {
ConnectionUtility.getInstance().startService(this);
super.onPause();
}
@Override
public void onDestroy() {
ConnectionUtility.getInstance().startService(this);
super.onDestroy();
}
@Override
public void onStop() {
ConnectionUtility.getInstance().startService(this);
super.onStop();
}
:
public void startService(Activity activity) {
if (!isMyServiceRunning(activity)) {
Intent intent = new Intent(activity, BackgroundUpdateService.class);
activity.startService(intent);
}
}
참고 : isMyServiceRunning() 메소드가 나는 서비스가 있는지 확인하기 위해 다른 곳에서 찾을 사용자 지정 방법을 달리는. 이 방법은 제가 아는 한까지 작동합니다.
이제이 서비스의 목적은 onPause(), onDestroy() 또는 onStop()을 통해 특정 Activity가 종료 될 때마다 println()을 트리거하고 onCreate() 또는 onResume 이 서비스는과 같이 중지하는 것입니다
@Override
public void onResume() {
ConnectionUtility.getInstance().stopServiceIfRunning(this);
super.onResume();
}
이 stopServiceIfRunning()로 한 단계 더 깊이 간다 :
public void stopServiceIfRunning(Activity activity) {
if (isMyServiceRunning(activity)) {
Intent intent = new Intent(activity, BackgroundUpdateService.class);
activity.stopService(intent);
}
}
지금 여기서 문제 야 : 나는 다시 밖으로 괜찮아 서비스를 시작할 수 있습니다 홈/뒤로 버튼을 통한 작업 또는 다른 Activity로 전환하면 startSer vice() 메서드가 실행되고 완벽하게 실행됩니다. 즉, "안녕하세요, 세상!" 필요에 따라 매 5 초마다 인쇄됩니다. 하지만 다시 Activity로 돌아 가면 내 맞춤 stopServiceIfRunning() 메소드가 실행되고 코드가 IF 블록 (확인 됨)에 들어가는 것을 확인할 수 있지만 서비스가 중지되지 않고 "Hello, World!" 오는. 내가 도대체 뭘 잘못하고있는 겁니까?
는 --- 쌍 날개의 천사의 코멘트에 대한 응답으로 편집 --- :
private boolean isMyServiceRunning(Activity activity) {
ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (BackgroundUpdateService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
그러나 다시,이 방법은 정확하게 지금까지 true와 false 값을 반환, 내가 생각하지 않는이 문제입니다 .
제발 게시 isMyServiceRunning() 내용 –