2013-08-15 8 views
1

나는 서비스를 시작하는 활동이 있습니다. 자, 나는 화면을 끄고 듣는 BroadcastReceiver에서 서비스를 시작/중단하고 싶다. 나는 다음을 사용했다 :BroadcastReceiver에서 서비스를 시작/중지 할 수 없습니다.

public class ScreenReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
     Log.d("Screen:", "In Method: ACTION_SCREEN_OFF"); 
     context.stopService(new Intent(context, ScreenReceiver.class)); 
    } 
    else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { 
     Log.d("Screen:", "In Method: ACTION_USER_PRESENT"); 
     context.startService(new Intent(context, ScreenReceiver.class)); 
    } 
} 
} 

나는 필요한 권한을 추가했고 서비스에 BroadcastReceiver를 등록/등록 취소했다. 코드를 실행할 때 Logcat에서 로그 메시지를 볼 수 있지만 서비스가 중지 된 것처럼 보이지 않습니다. 어떻게 해결해야합니까? 미리 감사드립니다.

+1

초 매개 변수로 startService''대신'ScreenReceiver' (브로드 캐스트 리시버 이름)의 서비스 패스 서비스 이름을 시작 AndroidManifest 파일에 서비스를 추가했는지 확인하십시오. –

+0

서비스 정의 방법은 무엇입니까? context.stopService (새로운 인 텐트 (context, ScreenReceiver.class)); 두 번째 인수는 YOURSERVICE이어야합니다. – kvh

답변

4

BroadcastReceiver를 다시 시작하려고합니다.
서비스 클래스를 대신 사용하십시오.

context.startService(new Intent(context, ScreenReceiver.class)); 

해야

context.startService(new Intent(context, YourService.class)); 

및 매니페스트 파일에

:

<service android:name=".YourService"></service> 
+0

그는 단지 – kvh

+0

@kevinhoo를 중지하는 데 문제가 있습니다.이 코드는 서비스를 시작하지 않습니다. 아마 다른 곳에서 서비스를 시작했을 것입니다. –

+0

젠장. 내 잘못이야. 감사. – Sriniketh

관련 문제