Android開發——關於Service的一些要點

來源:互聯網
上載者:User

service 有兩種調用形式:被啟動(startService)和被綁定(bindService)。前者要求在Service類中實現onStartCommand方法,Service啟動後需要手動停止,否則永遠運行下去;後者要求實現onBind方法,當沒有組件綁定到此Service時,它自行了斷。
Service需要在manifest檔案裡聲明。通過使用intent-filter可以使得其他程式啟動這一Service。
注意!Service在當前application所在的進程中運行,建議自行在Service中建立新線程來執行任務,否則有可能導致application出現“卡”的現象。直接使用 IntentService即可避免這一問題。它甚至已經實現一個任務隊列來解決並發的問題。如果確實需要並發的功能則只能自行繼承實現Service類了。
 onStartCommand()的傳回值包括START_NOT_STICKY,START_STICKY,START_REDELIVER_INTENT。詳情用力點擊相應連結。
如果在用startService啟動Service時希望Service返回一個結果,那麼可以用 PendingIntent作為參數,利用broadcast擷取傳回值。

當我們需要與Service進行互動或者希望把部分功能暴露給其他程式的時候,使用bindService。在Service中需實現onBind方法,返回一個IBinder介面,我們需要在此介面中定義一些方法,以便綁定Service的組件能夠通過這些方法與Service進行互動。使用unBindService解除綁定。
Service可使用Toast Notifications 或 Status Bar Notifications來通知使用者一些資訊。

當一個Service的存在是使用者能意識到的,如播放器,那麼在啟動service時使用 startForeground()。範例代碼如下:
[java]
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),System.currentTimeMillis());<br />Intent notificationIntent = new Intent(this, ExampleActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(this, getText(R.string.notification_title), 
        getText(R.string.notification_message), pendingIntent); 
startForeground(ONGOING_NOTIFICATION, notification); 


關於Service的生命週期:
[java]
public class ExampleService extends Service { 
    int mStartMode;       // indicates how to behave if the service is killed 
    IBinder mBinder;      // interface for clients that bind 
    boolean mAllowRebind; // indicates whether onRebind should be used 
 
    @Override 
    public void onCreate() { 
        // The service is being created 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
        // The service is starting, due to a call to startService() 
        return mStartMode; 
    } 
    @Override 
    public IBinder onBind(Intent intent) { 
        // A client is binding to the service with bindService() 
        return mBinder; 
    } 
    @Override 
    public boolean onUnbind(Intent intent) { 
        // All clients have unbound with unbindService() 
        return mAllowRebind; 
    } 
    @Override 
    public void onRebind(Intent intent) { 
        // A client is binding to the service with bindService(), 
        // after onUnbind() has already been called 
    } 
    @Override 
    public void onDestroy() { 
        // The service is no longer used and is being destroyed 
    } 
} 

與Activity不同的是,我們再重寫時不需要調用這些方法的super實現

最近剛剛使用了Service,感覺關係還是挺混亂的。自己實現的Service類,介面,Listener和Binder,暫時還是覺得沒搞透徹,需要繼續熟悉一下。

 


摘自 tangwing的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.