IntentService

      在Android開發中,我們或許會碰到這麼一種業務需求,一項任務分成幾個子任務,子任務按順序先後執行,子任務全部執行完後,這項任務纔算成功。那麼,利用幾個子線程順序執行是可以達到這個目的的,但是每個線程必須去手動控制,而且得在一個子線程執行完後,再開啓另一個子線程。或者,全部放到一個線程中讓其順序執行。這樣都可以做到,但是,如果這是一個後臺任務,就得放到Service裏面,由於Service和Activity是同級的,所以,要執行耗時任務,就得在Service裏面開子線程來執行。那麼,有沒有一種簡單的方法來處理這個過程呢,答案就是IntentService。
      什麼是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);  
    }  
  
//設置當進程被重建時是否需要重新接受意圖,true表示需要,並且當有多個意圖時,只能保證最近的一個可以收到
    @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> 
      最後來看看執行結果:
      07-30 17:20:49.938: V/IntentServiceDemo(20465):onCreate
      07-30 17:20:49.948: V/IntentServiceDemo(20465): onStartCommand
      07-30 17:20:49.948: V/IntentServiceDemo(20465): onStart
      07-30 17:20:49.948: V/IntentServiceDemo(20465): onStartCommand
      07-30 17:20:49.948: V/IntentServiceDemo(20465): onStart
      07-30 17:20:49.948: V/IntentServiceDemo(20465): Operation1
      07-30 17:20:51.958: V/IntentServiceDemo(20465): Operation2
      07-30 17:20:53.958: V/IntentServiceDemo(20465): onDestroy

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


轉載自http://www.2cto.com/kf/201409/331983.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章