Android中IntentService詳解

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   io   ar   使用   

簡單說,IntentService是繼承於Service並處理非同步請求的一個類,在IntentService內有一個背景工作執行緒來處理耗時操作,啟動IntentService的方式和啟動傳統Service一樣,同時,當任務執行完後,IntentService會自動停止,而不需要我們去手動控制。另外,可以啟動IntentService多次,而每一個耗時操作會以工作隊列的方式在IntentService的onHandleIntent回調方法中執行,並且,每次只會執行一個背景工作執行緒,執行完第一個再執行第二個,以此類推。

而且,所有請求都在一個單線程中,不會阻塞應用程式的主線程(UI Thread),同一時間只處理一個請求。

那麼,用IntentService有什麼好處呢?首先,我們省去了在Service中手動開線程的麻煩,第二,當操作完成時,我們不用手動停止Service。

接下來讓我們來看看如何使用,寫一個Demo來類比兩個耗時操作,Operation1與Operation2,先執行1,2必須等1執行完才能執行:

 

建立工程,建立一個繼承IntentService的類,我這裡是IntentServiceDemo.java

public class IntentServiceDemo extends IntentService {    public IntentServiceDemo() {        //必須實現父類的構造方法        super("IntentServiceDemo");    }        @Override    public IBinder onBind(Intent intent) {        System.out.println("onBind");        return super.onBind(intent);    }    @Override    public void onCreate() {        System.out.println("onCreate");        super.onCreate();    }    @Override    public void onStart(Intent intent, int startId) {        System.out.println("onStart");        super.onStart(intent, startId);    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        System.out.println("onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void setIntentRedelivery(boolean enabled) {        super.setIntentRedelivery(enabled);        System.out.println("setIntentRedelivery");    }    @Override    protected void onHandleIntent(Intent intent) {        //Intent是從Activity發過來的,攜帶識別參數,根據參數不同執行不同的任務        String action = intent.getExtras().getString("param");        if (action.equals("oper1")) {            System.out.println("Operation1");        }else if (action.equals("oper2")) {            System.out.println("Operation2");        }                try {            Thread.sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    @Override    public void onDestroy() {        System.out.println("onDestroy");        super.onDestroy();    }}

 

我把生命週期方法全列印出來了,待會我們來看看它執行的過程是怎樣的。接下來是Activity,在Activity中來啟動IntentService:

public class TestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //可以啟動多次,每啟動一次,就會建立一個work thread,但IntentService的執行個體始終只有一個        //Operation 1        Intent startServiceIntent = new Intent("com.test.intentservice");        Bundle bundle = new Bundle();        bundle.putString("param", "oper1");        startServiceIntent.putExtras(bundle);        startService(startServiceIntent);                //Operation 2        Intent startServiceIntent2 = new Intent("com.test.intentservice");        Bundle bundle2 = new Bundle();        bundle2.putString("param", "oper2");        startServiceIntent2.putExtras(bundle2);        startService(startServiceIntent2);    }}

  

最後,別忘了配置Service,因為它繼承於Service,所以,它還是一個Service,一定要配置,否則是不起作用的,開始我就是忘了,結果半天沒反應。

<service android:name=".IntentServiceDemo">      <intent-filter >          <action android:name="com.test.intentservice"/>      </intent-filter></service>

  

ok,最後來看看執行結果:

從結果可以看到,onCreate方法只執行了一次,而onStartCommand和onStart方法執行了兩次,開啟了兩個Work Thread,這就證實了之前所說的,啟動多次,但IntentService的執行個體只有一個,這跟傳統的Service是一樣的。Operation1也是先於Operation2列印,並且我讓兩個操作間停頓了2s,最後是onDestroy銷毀了IntentService。

 

這就是IntentService,一個方便我們處理商務程序的類,它是一個Service,但是比Service更智能。

 

Android中IntentService詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.