Android中IntentService詳解


簡單說,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更智能。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM