나는 유해 컨텐츠 차단 애플리케이션/성인용 컨텐츠 필터링 애플리케이션을 개발 중입니다. 이 응용 프로그램은 지속적으로 어린이의 모바일 통화 및 smses을 모니터링하고 서버에 모든 활동을 기록합니다. 이를 위해 BOOT_COMPLETED에 서비스 (MyService.java) 을 시작하고 서비스의 onCreate 메소드에서 callLog 및 sms uri에 대한 contentobserver를 등록합니다 (아래 코드 스 니펫 참조).Android 서비스에 ContentObserver 등록
이제 나가는 모든 수신 전화와 SMS를 모니터링하고 싶기 때문에 서비스가 계속 실행되고 싶습니다 (중지/종료되지 않고). 또한이 서비스는 콘텐츠 관찰자를 등록하고 다른 처리를 수행하지 않고 (OnstartCommand 메서드가 더미 임) 사용되기 때문에 안드로이드 OS가 언젠가 서비스를 종료합니다. 서비스가 지속적으로 실행되고 contentobserver 객체가 활성 상태로 유지되도록하려면 어떻게해야합니까?
public class MyService extends Service { private CallLogObserver clLogObs = null; public void onCreate() { super.onCreate(); try{ clLogObs = new CallLogObserver(this); this.getContentResolver().registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true, clLogObs); }catch(Exception ex) { Log.e("CallLogData", ex.toString()); } } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { if(clLogObs !=null ) { this.getContentResolver().unregisterContentObserver(clLogObs); } super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return Service.START_STICKY; } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); }
사람들은 이미 서비스를 계속 실행할 수 없다는 의견에 댓글을 달았지만 서비스는 ContentObservers를 등록하는 것처럼 들리므로 서비스가 종료 될 때 해당 옵저버가 등록되어 있는지 여부가 중요한 질문 인 것처럼 들립니다. 새로운 관찰자 인스턴스를 다시 등록하지 않도록하는 방법에 대해 설명합니다. – spaaarky21