天天看點

android service總結

1、通過startservice方法啟動一個服務。service不能自己啟動自己。若在一個服務中啟動一個activity則,必須是申明一個全新的activity任務task。通過startservice方法啟動的服務不會随着啟動元件的消亡而消亡,而是一直執行着。

service生命周期 oncreate()-------->onstartcommand()----------->ondestroy() 

startservice()啟動一個服務後。若在該服務做耗時操作且沒有寫線程,則會導緻主線程堵塞!

服務啟動執行後會一直執行onstartcommand()方法。

2、用bindservice啟動一個服務,該服務和activity是綁定在一起的:啟動時,先調用oncreate()------>onbind()--------->onserviceconnected(),啟動服務的元件消亡,服務也就消亡了。

3、aidl服務調用方式

1)服務端代碼:

首先定義一個接口描寫叙述語言的接口:

然後定義服務元件代碼:

服務端的清單檔案:

> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_aidl_service" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.android_aidl_service.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name="com.example.android_aidl_service.myservice" > <intent-filter> <!-- 意圖過濾器要把aidl包名加類名 --> <action android:name="com.example.service.dataservice"/> </intent-filter> </service> </application> </manifest>

client編碼:

把aidl檔案包複制到client。client代碼例如以下:

這樣就能夠完畢程序間通信了。

4、用bindservice啟動服務并訪問本地服務的方法。

訪問界面代碼:

服務元件代碼:

通過該demo能夠訪問本地服務裡面的方法。

5、intentservice

本質是開啟一個線程來完畢耗時操作。

intentservice生命周期:

oncreate()------->onstartcommand()--------->onhandleintent()--------->ondestroy()

*/ public class downloadservice extends intentservice { @override public void oncreate() { // todo auto-generated method stub super.oncreate(); } public downloadservice() { super("downloadservice"); } // 僅僅需複寫例如以下方法 // 在該方法中運作操作 @override protected void onhandleintent(intent intent) { // 獲得提取網絡資源的執行個體 httpclient httpclient = new defaulthttpclient(); // 設定請求方式 httppost httppost = new httppost(intent.getstringextra("url")); // 設定存儲路徑 file file = new file(environment.getexternalstoragedirectory(), "intentservice.gif"); // 定義輸出流用于寫 fileoutputstream fileoutputstream = null; byte[] data = null;// 網絡資料 try { // 運作請求獲得響應 httpresponse httpresponse = httpclient.execute(httppost); // 推斷響應狀态碼 if (httpresponse.getstatusline().getstatuscode() == 200) { // 獲得響應實體 httpentity httpentity = httpresponse.getentity(); // 獲得網絡資料 data = entityutils.tobytearray(httpentity); // 推斷sd卡是否可用 if (environment.getexternalstoragestate().equals( environment.media_mounted)) { // 寫入sd卡 fileoutputstream=new fileoutputstream(file); fileoutputstream.write(data, 0, data.length); //toast.maketext( downloadservice.this,"下載下傳完畢", toast.length_long).show(); toast.maketext( getapplicationcontext(),"下載下傳完畢", toast.length_long).show(); } } } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { if (fileoutputstream != null) { try { fileoutputstream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } }

調用服務界面: