Android基本功:IntentService的使用

來源:互聯網
上載者:User

一、IntentService簡介

IntentService是Service的子類,比普通的Service增加了額外的功能。先看Service本身存在兩個問題:

  • Service不會專門啟動一條單獨的進程,Service與它所在應用位於同一個進程中;

  • Service也不是專門一條新線程,因此不應該在Service中直接處理耗時的任務;

    二、IntentService特徵

    • 會建立獨立的worker線程來處理所有的Intent請求;

    • 會建立獨立的worker線程來處理onHandleIntent()方法實現的代碼,無需處理多線程問題;

    • 所有請求處理完成後,IntentService會自動停止,無需調用stopSelf()方法停止Service;

    • 為Service的onBind()提供預設實現,返回null;

    • 為Service的onStartCommand提供預設實現,將請求Intent添加到隊列中;

      三、使用步驟(詳情參考Service項目)

      1. 繼承IntentService類,並重寫onHandleIntent()方法即可;

        MainActivity.java檔案

        public class MainActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      public void startService(View source) {         // 建立所需要啟動的Service的Intent         Intent intent = new Intent(this, MyService.class);         startService(intent);     }      public void startIntentService(View source) {         // 建立需要啟動的IntentService的Intent         Intent intent = new Intent(this, MyIntentService.class);         startService(intent);     } } 

        MyIntentService.java檔案

        public class MyIntentService extends IntentService {      public MyIntentService() {         super("MyIntentService");     }      @Override     protected void onHandleIntent(Intent intent) {         // IntentService會使用單獨的線程來執行該方法的代碼         // 該方法內執行耗時任務,比如下載檔案,此處只是讓線程等待20秒         long endTime = System.currentTimeMillis() + 20 * 1000;         System.out.println("onStart");         while (System.currentTimeMillis() < endTime) {             synchronized (this) {                 try {                     wait(endTime - System.currentTimeMillis());                 } catch (InterruptedException e) {                     e.printStackTrace();                 }             }         }         System.out.println("----耗時任務執行完成---");     } } 

        MyService.java檔案

        public class MyService extends Service {      @Override     public IBinder onBind(Intent arg0) {         return null;     }      @Override     public int onStartCommand(Intent intent, int flags, int startId) {         // 該方法內執行耗時任務可能導致ANR(Application Not Responding)異常         long endTime = System.currentTimeMillis() + 20 * 1000;         System.out.println("onStart");         while (System.currentTimeMillis() < endTime) {             synchronized (this) {                 try {                     wait(endTime - System.currentTimeMillis());                 } catch (InterruptedException e) {                     e.printStackTrace();                 }             }         }         System.out.println("----耗時任務執行完成---");         return START_STICKY;     } } 
        運行上述代碼,啟動MyIntentService的會使用單獨的worker線程,因此不會阻塞前台的UI線程;而MyService會

聯繫我們

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