天天看點

Android Service學習之本地服務

    service是在一段不定的時間運作在背景,不和使用者互動應用元件。每個service必須在manifest中 通過<service>來聲明。可以通過contect.startservice和contect.bindserverice來啟動。

    service和其他的應用元件一樣,運作在程序的主線程中。這就是說如果service需要很多耗時或者阻塞的操作,需要在其子線程中實作。

    service的兩種模式(startservice()/bindservice()不是完全分離的):

本地服務 local service 用于應用程式内部。

  它可以啟動并運作,直至有人停止了它或它自己停止。在這種方式下,它以調用context.startservice()啟動,而以調用context.stopservice()結束。它可以調用service.stopself() 或 service.stopselfresult()來自己停止。不論調用了多少次startservice()方法,你隻需要調用一次stopservice()來停止服務。

  用于實作應用程式自己的一些耗時任務,比如查詢更新資訊,并不占用應用程式比如activity所屬線程,而是單開線程背景執行,這樣使用者體驗比較好。

遠端服務 remote service 用于android系統内部的應用程式之間。

  它可以通過自己定義并暴露出來的接口進行程式操作。用戶端建立一個到服務對象的連接配接,并通過那個連接配接來調用服務。連接配接以調用context.bindservice()方法建立,以調用 context.unbindservice()關閉。多個用戶端可以綁定至同一個服務。如果服務此時還沒有加載,bindservice()會先加載它。

  可被其他應用程式複用,比如天氣預報服務,其他應用程式不需要再寫這樣的服務,調用已有的即可。

生命周期

    service的生命周期并不像activity那麼複雜,它隻繼承了oncreate(),onstart(),ondestroy()三個方法,當我們第一次啟動service時,先後調用了oncreate(),onstart()這兩個方法,當停止service時,則執行ondestroy()方法,這裡需要注意的是,如果service已經啟動了,當我們再次啟動service時,不會在執行oncreate()方法,而是直接執行onstart()方法。

    而啟動service,根據onstartcommand的傳回值不同,有兩個附加的模式:

    1. start_sticky 用于顯示啟動和停止service。

    2. start_not_sticky或start_redeliver_intent用于有指令需要處理時才運作的模式。

    服務不能自己運作,需要通過調用context.startservice()或context.bindservice()方法啟動服務。這兩個方法都可以啟動service,但是它們的使用場合有所不同。

    1. 使用startservice()方法啟用服務,調用者與服務之間沒有關連,即使調用者退出了,服務仍然運作。

    如果打算采用context.startservice()方法啟動服務,在服務未被建立時,系統會先調用服務的oncreate()方法,接着調用onstart()方法。

    如果調用startservice()方法前服務已經被建立,多次調用startservice()方法并不會導緻多次建立服務,但會導緻多次調用onstart()方法。

    采用startservice()方法啟動的服務,隻能調用context.stopservice()方法結束服務,服務結束時會調用ondestroy()方法。

    2. 使用bindservice()方法啟用服務,調用者與服務綁定在了一起,調用者一旦退出,服務也就終止,大有“不求同時生,必須同時死”的特點。

    onbind()隻有采用context.bindservice()方法啟動服務時才會回調該方法。該方法在調用者與服務綁定時被調用,當調用者與服務已經綁定,多次調用context.bindservice()方法并不會導緻該方法被多次調用。

    采用context.bindservice()方法啟動服務時隻能調用onunbind()方法解除調用者與服務解除,服務結束時會調用ondestroy()方法。

看看官方給出的比較流程示意圖:

Android Service學習之本地服務

    官方文檔告訴我們,一個service可以同時start并且bind,在這樣的情況,系統會一直保持service的運作狀态如果service已經start了或者bind_auto_create标志被設定。如果沒有一個條件滿足,那麼系統将會調用ondestory方法來終止service.所有的清理工作(終止線程,反注冊接收器)都在ondestory中完成。

擁有service的程序具有較高的優先級

    官方文檔告訴我們,android系統會盡量保持擁有service的程序運作,隻要在該service已經被啟動(start)或者用戶端連接配接(bindservice)到它。當記憶體不足時,需要保持,擁有service的程序具有較高的優先級。

1. 如果service正在調用oncreate,onstartcommand或者ondestory方法,那麼用于目前service的程序則變為前台程序以避免被killed。

2. 如果目前service已經被啟動(start),擁有它的程序則比那些使用者可見的程序優先級低一些,但是比那些不可見的程序更重要,這就意味着service一般不會被killed.

3. 如果用戶端已經連接配接到service (bindservice),那麼擁有service的程序則擁有最高的優先級,可以認為service是可見的。

4. 如果service可以使用startforeground(int, notification)方法來将service設定為前台狀态,那麼系統就認為是對使用者可見的,并不會在記憶體不足時killed。

如果有其他的應用元件作為service,activity等運作在相同的程序中,那麼将會增加該程序的重要性。

本地service

1.不需和activity互動的本地服務

public class localservice extends service {

        private static final string tag = "localservice";

        @override

        public ibinder onbind(intent intent) {

                log.i(tag, "onbind");

                return null;

        }

        public void oncreate() {

                log.i(tag, "oncreate");

                super.oncreate();

        public void ondestroy() {

                log.i(tag, "ondestroy");

                super.ondestroy();

        public void onstart(intent intent, int startid) {

                log.i(tag, "onstart");

                super.onstart(intent, startid);

}

activity:

public class serviceactivity extends activity {

        protected void oncreate(bundle savedinstancestate) {

                super.oncreate(savedinstancestate);

                setcontentview(r.layout.servicedemo);

                ((button) findviewbyid(r.id.startlocalservice)).setonclicklistener(

                                new view.onclicklistener(){

                                        @override

                                        public void onclick(view view) {

                                                // todo auto-generated method stub

                                               startservice(new intent("com.demo.service_demo"));

                                        }

                                });

                ((button) findviewbyid(r.id.stoplocalservice)).setonclicklistener(

                                                stopservice(new intent("com.demo.service_demo"));

                                        }

在androidmanifest.xml添加:

<service android:name=".localservice">

        <intent-filter>

                <action android:name="com.demo.service_demo" />

                <category android:name="android.intent.category.default" />

        </intent-filter>

</service>

否則啟動服務時會提示new intent找不到"com.demo.service_demo"。

    對于這類不需和activity互動的本地服務,是使用startservice/stopservice的最好例子。

    運作時可以發現第一次startservice時,會調用oncreate和onstart,在沒有stopservice前,無論點選多少次startservice,都隻會調用onstart。而stopservice時調用ondestroy。再次點選stopservice,會發現不會進入service的生命周期的,即不會再調用oncreate,onstart和ondestroy。

    而onbind在startservice/stopservice中沒有調用。

2.本地服務和activity互動

    對于這種case,官方的sample(apidemo\app.localservice)是最好的例子:

/**

* this is an example of implementing an application service that runs locally

* in the same process as the application.    the {@link localservicecontroller}

* and {@link localservicebinding} classes show how to interact with the

* service.

*

* <p>notice the use of the {@link notificationmanager} when interesting things

* happen in the service.    this is generally how background services should

* interact with the user, rather than doing something more disruptive such as

* calling startactivity().

*/

        private notificationmanager mnm;

        /**

         * class for clients to access.    because we know this service always

         * runs in the same process as its clients, we don't need to deal with

         * ipc.

         */

        public class localbinder extends binder {

                localservice getservice() {

                        return localservice.this;

                }

                mnm = (notificationmanager)getsystemservice(notification_service);

                // display a notification about us starting.    we put an icon in the status bar.

                shownotification();

        public int onstartcommand(intent intent, int flags, int startid) {

                log.i("localservice", "received start id " + startid + ": " + intent);

                // we want this service to continue running until it is explicitly

                // stopped, so return sticky.

                return start_sticky;

                // cancel the persistent notification.

                mnm.cancel(r.string.local_service_started);

                // tell the user we stopped.

                toast.maketext(this, r.string.local_service_stopped, toast.length_short).show();

             return mbinder;

        // this is the object that receives interactions from clients.    see

        // remoteservice for a more complete example.

        private final ibinder mbinder = new localbinder();

         * show a notification while this service is running.

        private void shownotification() {

                // in this sample, we'll use the same text for the ticker and the expanded notification

                charsequence text = gettext(r.string.local_service_started);

                // set the icon, scrolling text and timestamp

                notification notification = new notification(r.drawable.stat_sample, text,

                                system.currenttimemillis());

                // the pendingintent to launch our activity if the user selects this notification

                pendingintent contentintent = pendingintent.getactivity(this, 0,

                                new intent(this, localservicecontroller.class), 0);

                // set the info for the views that show in the notification panel.

                notification.setlatesteventinfo(this, gettext(r.string.local_service_label),

                                             text, contentintent);

                // send the notification.

                // we use a layout id because it is a unique number.    we use it later to cancel.

                mnm.notify(r.string.local_service_started, notification);

   這裡可以發現onbind需要傳回一個ibinder對象。也就是說和上一例子localservice不同的是,

1. 添加了一個public内部類繼承binder,并添加getservice方法來傳回目前的service對象;

2. 建立一個ibinder對象——new那個binder内部類;

3. onbind方法返還那個ibinder對象。

* <p>example of binding and unbinding to the {@link localservice}.

* this demonstrates the implementation of a service which the client will

* bind to, receiving an object through which it can communicate with the service.</p>

public class localservicebinding extends activity {

        private boolean misbound;

        private localservice mboundservice;

        protected void oncreate(bundle savedinstancestate) {

                setcontentview(r.layout.local_service_binding);

                // watch for button clicks.

                button button = (button)findviewbyid(r.id.bind);

                button.setonclicklistener(mbindlistener);

                button = (button)findviewbyid(r.id.unbind);

                button.setonclicklistener(munbindlistener);

        private serviceconnection mconnection = new serviceconnection() {

                public void onserviceconnected(componentname classname, ibinder service) {

                        // this is called when the connection with the service has been

                        // established, giving us the service object we can use to

                        // interact with the service.    because we have bound to a explicit

                        // service that we know is running in our own process, we can

                        // cast its ibinder to a concrete class and directly access it.

                        mboundservice = ((localservice.localbinder)service).getservice();  

                        // tell the user about this for our demo.

                        toast.maketext(localservicebinding.this, r.string.local_service_connected,

                                        toast.length_short).show();

                public void onservicedisconnected(componentname classname) { 

                        // unexpectedly disconnected -- that is, its process crashed.

                        // because it is running in our same process, we should never

                        // see this happen.

                        mboundservice = null;

                        toast.maketext(localservicebinding.this, r.string.local_service_disconnected,

        };

        private onclicklistener mbindlistener = new onclicklistener() {

                public void onclick(view v) {

                        // establish a connection with the service.    we use an explicit

                        // class name because we want a specific service implementation that

                        // we know will be running in our own process (and thus won't be

                        // supporting component replacement by other applications).

                        bindservice(new intent(localservicebinding.this,    

                                        localservice.class), mconnection, context.bind_auto_create);

                        misbound = true;

        private onclicklistener munbindlistener = new onclicklistener() {

                        if (misbound) {

                                // detach our existing connection.

                                unbindservice(mconnection);

                                misbound = false;

                        }

    明顯看出這裡面添加了一個名為serviceconnection類,并實作了onserviceconnected(從ibinder擷取service對象)和onservicedisconnected(set service to null)。

    而bindservice和unbindservice方法都是操作這個serviceconnection對象的。

androidmanifest.xml裡添加:

<service android:name=".app.localservice" />

<activity android:name=".app.localservicebinding" android:label="@string/activity_local_service_binding">

     <intent-filter>

          <action android:name="android.intent.action.main" />

          <category android:name="android.intent.category.sample_code" />

     </intent-filter>

</activity>

這裡沒什麼特别的,因為service沒有需要什麼特别的action,是以隻是聲明service而已,而activity和普通的沒差别。

運作時,發現調用次序是這樣的:

bindservice:

1.localservice : oncreate

2.localservice : onbind

3.activity: onserviceconnected

unbindservice: 隻是調用ondestroy

可見,onstart是不會被調用的,而onservicedisconnected沒有調用的原因在上面代碼的注釋有說明。

介紹onstartcommand()需要用到的幾個常量 (引自官方文檔)

start_not_sticky<dl></dl>

<dd></dd>

if the system kills the service after onstartcommand() returns, do not recreate the service, unless there are pending intents to deliver. this is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.

<dt>start_sticky</dt>

if the system kills the service after onstartcommand() returns, recreate the service and call onstartcommand(), but do not redeliver the last intent. instead, the system calls onstartcommand() with a null intent, unless there were pending intents to start the service, in which case, those intents are delivered. this is suitable for media players (or similar services) that are not executing commands, but running indefinitely and waiting for a job.

<dt>start_redeliver_intent</dt>

<dd>if the system kills the service after onstartcommand() returns, recreate the service and call onstartcommand() with the last intent that was delivered to the service. any pending intents are delivered in turn. this is suitable for services that are actively performing a job that should be immediately resumed, such as do wnloading a file.</dd>

running a service in the foreground

    具體内容檢視官方文檔,主要是使用 startforeground() 和 stopforeground()方法。