2012-03-31 23 views
1

我已經編寫了一個包含服務類和活動的應用程序。如何在後臺運行我的應用程序24X7,用於偵聽聯繫人數據庫中的更改

我從活動開始了服務這樣

public class RecycleBinActivity extends Activity { 


    /** Called when the activity is first created. */ 


    Uri cpath=null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     cpath=ContactsContract.Contacts.CONTENT_URI; 
     this.getApplicationContext().getContentResolver().registerContentObserver(cpath, true, observer); 

    } 
    private class MyContentObserver extends ContentObserver { 

     public MyContentObserver() { 
      super(null); 
     } 

     @Override 
     public void onChange(boolean selfChange) { 
      super.onChange(selfChange); 
     Intent i=new Intent(RecycleBinActivity.this.getApplicationContext(),DeleteService.class); 
     startService(i); 
    } 
     @Override 
     public boolean deliverSelfNotifications() 
     { 
      super.deliverSelfNotifications(); 
      return true; 

     } 

    } 
    MyContentObserver observer=new MyContentObserver(); 

} 

和我的服務實現如下所示

public class DeleteService extends Service { 

    private final IBinder mBinder = new LocalBinder(); 
    Notification nf; 
    NotificationManager nfm; 
    Uri cpath,lookupuri; 
    String lookupkey; 
    Cursor cur; 
    Runnable refresher; 
    ContentResolver cr; 
    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return mBinder; 
    } 
    @Override 
    public void onCreate() { 

     cpath=ContactsContract.Contacts.CONTENT_URI; 


         // some action 

       nfm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
        int NOTIFICATION_ID = 1; 
      Intent intent = new Intent(); 
      PendingIntent pi = PendingIntent.getActivity(DeleteService.this, 1, intent, 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; 
        startForeground(NOTIFICATION_ID, nf); 

      } 






    @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); 
    return Service.START_STICKY; 
    } 
    public class LocalBinder extends Binder { 
      DeleteService getService() { 
       return DeleteService.this; 
      } 

    } 
} 

當我運行這個程序,並修改接觸它顯示通知首次 如果我幾秒鐘再次修改它不起作用爲什麼請任何人幫我

回答

2

因爲onCreate方法通常被調用一次。

實際上,不能保證你的應用程序將全天候工作,因爲操作系統可能會殺死它,如果內存不足。當然,如果你的服務是「粘性」的,它將被重新創建(所以,它幾乎是24x7)。

+0

具有從開機啓動的前臺服務有點24X7服務。 – 2016-01-06 02:30:20

2

將您所有的邏輯移動到onStartCommand,您希望每次進行更改時都會發生這種情況。因爲onCreate被第一次執行服務時調用,之後每次調用onStartCommand(直到服務的生命期),您執行startService

爲了讓它更加靈活地全天候運行,請確保您在處理啓動廣播後重新啓動電話後開始執行服務。

權限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

廣播:

<action android:name="android.intent.action.BOOT_COMPLETED" /> 
0

如果你想運行你的應用程序全天候,把在通知欄中一些圖標從的onCreate()您的應用程序。如果操作系統位於設備的通知欄並且不會終止它,操作系統會將其視爲優先進程。您可以聽取啓動完成以將您的應用程序圖標放入通知欄中。

start an application from notification bar in android

0

我知道爲時已晚:但是這是一個正確的答案,現在聽的聯繫人數據庫的變化:

使用Content Observer聽的聯絡表中的任何改變。

相關問題