android學習筆記9

來源:互聯網
上載者:User

Service:
     掌握進程內服務的管理方法;
     掌握服務的隱式啟動和顯示啟動;
     掌握跨進程服務的綁定和調用方法;
     進程之間通訊;
    
     綁定方式:資料共用方便;
               bindService()建立服務串連 和 unbindService()停止服務串連(Connection);
               如果沒有啟動bindService沒有調用,則會自動調用Service;
               同一個Service可以綁定多個服務串連;
              
     命令列安裝apk檔案:
          先啟動一個avd;使用命令adb install [apk檔案的完整路徑]
         
    
     寫一個Service,需要繼承Service類,啟動一個Service使用startService(Intent),
     結束Service,stopService(Intent)
    
      在manifest中註冊一個Service:
           <servcie />
    
     ServiceConnection的使用,使用bindService()方法進行Service綁定:
          1、在Service中的onBind方法中的返回的值不能為null;
          2、若Service中的onBind方法返回的是null,則在ServiceConnetion中的回呼函數
             onServiceConnected方法沒有反應;
          3、在Activity中調用unbindService方法
         
     Service:
          在Service中建立一個內部類,繼承Bundle類,其中建立一個方法返回Service對象,用以在activity中擷取
          Service對象,調用其他方法
         
          例如:
              class LocalService extends Binder {
       
        TestService getService() {
         return TestService.this;
        }
       }
    
  生命週期:
     startService   ===>   onCreate() --> onStart() ,第二次startService ==> onStart()
     stopService ===> onDestroy()
    
    
     bindService ===>  onCreate() --> onBind(),第二次bindService,不在調用onCreate(),onBind()方法,而調用
                                                ServiceConnection中的onServiceConnected方法
     unbindService ===> onDestroy()
    
    ① 使用startService方式無法擷取Service對象,bindService可以擷取;
    ② bindService方式對於Service的生命週期的控制比較弱;startService可以擷取在onStart中擷取一些資源

聯繫我們

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