2012-04-02 2 views
1

내 앱이 백그라운드에서 실행되도록하고 연락처, SMS 삭제 이벤트를 수신합니다. 그에 대한 내가 내 응용 프로그램에 서비스를 만들었지 만 내가 활동하지 않고 시작하는 방법을 DNT 내 코드의 활동이연락처 또는 SMS를 삭제할 때 활동 경고없이 사용자가 앱을 실행하십시오.

public class DeleteService extends Service { 

ContentResolver cr; 

MyContentObserver observer=new MyContentObserver(); 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return mBinder; 
} 
@Override 
public void onCreate() { 

    cpath=ContactsContract.Contacts.CONTENT_URI; 

       // some action 
     } 

@Override 

public void onDestroy() { 

} 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
// TODO Launch a background thread to do processing. 
    super.onStartCommand(intent, flags, startId); 
    cpath=ContactsContract.Contacts.CONTENT_URI; 
    cr=getContentResolver(); 
    cur=cr.query(cpath, null, null, null, null); 


    this.getApplicationContext().getContentResolver().registerContentObserver(cpath, true, observer); 
    return Service.START_STICKY; 

    } 
private class MyContentObserver extends ContentObserver { 

     public MyContentObserver() { 
      super(null); 
     } 

     @Override 
     public void onChange(boolean selfChange) { 
      super.onChange(selfChange); 
      nfm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      int NOTIFICATION_ID = 1; 
     Intent intent1 = new Intent(); 
     PendingIntent pi = PendingIntent.getActivity(DeleteService.this, 1, intent1, 0); 
     nf=new Notification(R.drawable.ic_launcher,"Contact Database changed",System.currentTimeMillis()); 
     nf.setLatestEventInfo(getApplicationContext(), "Delete Event", "contact name", pi); 
     nf.flags = nf.flags | 
      Notification.FLAG_ONGOING_EVENT; 
    } 
     @Override 
     public boolean deliverSelfNotifications() 
     { 
      super.deliverSelfNotifications(); 
      return true; 

     } 
} 
public class LocalBinder extends Binder { 
     DeleteService getService() { 
      return DeleteService.this; 
     } 

} 
} 
+0

답변이 적합한 경우 올바른 코드로 선택하십시오 – Akhil

답변

0

같다 .. 한 OnCreate

Intent svc=new Intent(youractivity.this,DeleteService.class); 
      startService(svc); 
1

서비스에이 코드를 넣어 Activity, BroadCast 리시버 또는 이미 시작된 서비스에 의해 시작되어 이 될 수 있습니다. 독립적 일 수는 없습니다 (단독으로 시작할 수는 없습니다). 따라서 시작하려면 두 구성 요소 중 하나가 필요합니다. 당신은 선호하는 방법 인 서비스를 시작하는 활동을 할 수 있습니다. 그러나 사용자 인터페이스를 제공하고 싶지 않다면, 전화가 켜지고 부팅이 완료되었을 때 작동하는 브로드 캐스트 수신기를 구현하십시오. 그 br 안에 서비스를 시작하십시오. 이것은 또한 전화가 시작 되 자마자 서비스를 실행하는 데 도움이됩니다.

<receiver android:name="com.my.MyBroadcastReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

과 BR의 : 매니페스트 예를 들면

public class MyBroadcastReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent i=new Intent(context,DeleteService.class); 
      context.startService(i); 
    } 

}

1

레지스터 ACTION_SCREEN_ON 또는 위해 appliction에 대한 ACTION_USER_PRESENT 방송 recivers 서비스 서비스 및 시작 화면이 켜져있을 때 또는 사용자가 있습니다. manifest.xml에서

:

<receiver android:name="com.my.AppStart"> 
    <intent-filter> 
     <action android:name="android.intent.action.SCREEN_ON" /> 
     <action android:name="android.intent.action.SCREEN_OFF" /> 
     <action android:name="android.intent.action.USER_PRESENT" /> 
    </intent-filter> 
</receiver> 

브로드 캐스트 리시버 :

public class AppStart extends BroadcastReceiver { 
public static final String present = "android.intent.action.USER_PRESENT"; 
public static final String screenon = "android.intent.action.SCREEN_ON"; 
public static final String screenoff = "android.intent.action.SCREEN_OFF"; 
    @Override 
    public void onReceive(Context context, Intent intent) { 

    if (intent.getAction().equals(present) || intent.getAction().equals(screenon)) 
    { 
     Intent i=new Intent(context,DeleteService.class); 
     context.startService(i); 
    } 
    if (intent.getAction().equals(screenoff)) 
    { 
     //STOP YOUR SERVICE HERE 
    } 

    } 
} 
휴대 전화 화면이 app.as에 의해 배터리 소모를 방지하기 위해 꺼져있을 때 당신을 stoping 서비스에 대한 ACTION_SCREEN_OFF 방송 reciver입니다을 등록 할 수 있습니다
+0

코드가 배터리 사용 편의성이 더 좋습니다. 이러한 의도에 대해 알지 못했습니다. 이러한 의도에 대해 이야기 해 주셔서 감사합니다. 하지만 다른 앱이 있다면. 사용자 화면이 꺼져있는 동안 breceiver가 발사하는 동안 프로그램 적으로 연락처를 수정하고 있습니까?] – Akhil

+0

매니페스트에 등록 된 경우에만 'SCREEN_ON', 'SCREEN_OFF' 인 텐트 필터가 작동하지 않습니다. 'USER_PRESENT' 필터는 실행중인 Android 버전에 잠금 화면이있는 경우 작동합니다. 4.x는 대부분의 장치에서'USER_PRESENT'로 잘 작동합니다. 이 기타 참조 : http://stackoverflow.com/questions/3601014/can-an-action-user-present-broadcastreceiver-not-be-registered-in-the-manifest – treejanitor

관련 문제