Android之Service
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1、Service的種類
??
按運(yùn)行地點(diǎn)分類:
| 類別 | 區(qū)別 | ?優(yōu)點(diǎn) | 缺點(diǎn)? | ?應(yīng)用 |
| 本地服務(wù)(Local) | 該服務(wù)依附在主進(jìn)程上, | ?服務(wù)依附在主進(jìn)程上而不是獨(dú)立的進(jìn)程,這樣在一定程度上節(jié)約了資源,另外Local服務(wù)因?yàn)槭窃谕贿M(jìn)程因此不需要IPC,也不需要AIDL。相應(yīng)bindService會(huì)方便很多。 | ?主進(jìn)程被Kill后,服務(wù)便會(huì)終止。 | ?非常常見的應(yīng)用如:HTC的音樂播放服務(wù),天天動(dòng)聽音樂播放服務(wù)。 |
| 遠(yuǎn)程服務(wù)(Remote) | 該服務(wù)是獨(dú)立的進(jìn)程, | ?服務(wù)為獨(dú)立的進(jìn)程,對應(yīng)進(jìn)程名格式為所在包名加上你指定的android:process字符串。由于是獨(dú)立的進(jìn)程,因此在Activity所在進(jìn)程被Kill的時(shí)候,該服務(wù)依然在運(yùn)行,不受其他進(jìn)程影響,有利于為多個(gè)進(jìn)程提供服務(wù)具有較高的靈活性。 | ?該服務(wù)是獨(dú)立的進(jìn)程,會(huì)占用一定資源,并且使用AIDL進(jìn)行IPC稍微麻煩一點(diǎn)。 | ?一些提供系統(tǒng)服務(wù)的Service,這種Service是常駐的。 |
其實(shí)remote服務(wù)還是很少見的,并且一般都是系統(tǒng)服務(wù)。
??
按運(yùn)行類型分類:
| 類別 | 區(qū)別 | 應(yīng)用 |
| 前臺(tái)服務(wù) | 會(huì)在通知一欄顯示 ONGOING 的 Notification, | 當(dāng)服務(wù)被終止的時(shí)候,通知一欄的 Notification 也會(huì)消失,這樣對于用戶有一定的通知作用。常見的如音樂播放服務(wù)。 |
| 后臺(tái)服務(wù) | 默認(rèn)的服務(wù)即為后臺(tái)服務(wù),即不會(huì)在通知一欄顯示?ONGOING 的?Notification。 | 當(dāng)服務(wù)被終止的時(shí)候,用戶是看不到效果的。某些不需要運(yùn)行或終止提示的服務(wù),如天氣更新,日期同步,郵件同步等。 |
有同學(xué)可能會(huì)問,后臺(tái)服務(wù)我們可以自己創(chuàng)建 ONGOING 的 Notification 這樣就成為前臺(tái)服務(wù)嗎?答案是否定的,前臺(tái)服務(wù)是在做了上述工作之后需要調(diào)用 startForeground (?android 2.0 及其以后版本?)或 setForeground (android 2.0 以前的版本)使服務(wù)成為 前臺(tái)服務(wù)。這樣做的好處在于,當(dāng)服務(wù)被外部強(qiáng)制終止掉的時(shí)候,ONGOING 的 Notification 任然會(huì)移除掉。
?
按使用方式分類:
| 類別 | 區(qū)別 |
| startService 啟動(dòng)的服務(wù) | 主要用于啟動(dòng)一個(gè)服務(wù)執(zhí)行后臺(tái)任務(wù),不進(jìn)行通信。停止服務(wù)使用stopService |
| bindService 啟動(dòng)的服務(wù) | 該方法啟動(dòng)的服務(wù)要進(jìn)行通信。停止服務(wù)使用unbindService |
| startService?同時(shí)也 bindService 啟動(dòng)的服務(wù) | 停止服務(wù)應(yīng)同時(shí)使用stepService與unbindService |
以上面三種方式啟動(dòng)的服務(wù)其生命周期也有區(qū)別,將在隨后給出。
?
2、Service 與 Thread 的區(qū)別
??
很多時(shí)候,你可能會(huì)問,為什么要用 Service,而不用 Thread 呢,因?yàn)橛?Thread 是很方便的,比起 Service 也方便多了,下面我詳細(xì)的來解釋一下。
?
1). Thread:Thread 是程序執(zhí)行的最小單元,它是分配CPU的基本單位。可以用 Thread 來執(zhí)行一些異步的操作。
2). Service:Service 是android的一種機(jī)制,當(dāng)它運(yùn)行的時(shí)候如果是Local Service,那么對應(yīng)的 Service 是運(yùn)行在主進(jìn)程的 main 線程上的。如:onCreate,onStart 這些函數(shù)在被系統(tǒng)調(diào)用的時(shí)候都是在主進(jìn)程的 main 線程上運(yùn)行的。如果是Remote Service,那么對應(yīng)的 Service 則是運(yùn)行在獨(dú)立進(jìn)程的 main 線程上。因此請不要把 Service 理解成線程,它跟線程半毛錢的關(guān)系都沒有!
??
既然這樣,那么我們?yōu)槭裁匆?Service 呢?其實(shí)這跟 android 的系統(tǒng)機(jī)制有關(guān),我們先拿 Thread 來說。Thread 的運(yùn)行是獨(dú)立于 Activity 的,也就是說當(dāng)一個(gè) Activity 被 finish 之后,如果你沒有主動(dòng)停止 Thread?或者 Thread 里的 run 方法沒有執(zhí)行完畢的話,Thread?也會(huì)一直執(zhí)行。因此這里會(huì)出現(xiàn)一個(gè)問題:當(dāng) Activity 被 finish 之后,你不再持有該 Thread 的引用。另一方面,你沒有辦法在不同的 Activity 中對同一 Thread 進(jìn)行控制。
??
舉個(gè)例子:如果你的 Thread 需要不停地隔一段時(shí)間就要連接服務(wù)器做某種同步的話,該 Thread?需要在 Activity 沒有start的時(shí)候也在運(yùn)行。這個(gè)時(shí)候當(dāng)你 start 一個(gè) Activity 就沒有辦法在該 Activity 里面控制之前創(chuàng)建的 Thread。因此你便需要?jiǎng)?chuàng)建并啟動(dòng)一個(gè) Service ,在 Service 里面創(chuàng)建、運(yùn)行并控制該 Thread,這樣便解決了該問題(因?yàn)槿魏?Activity 都可以控制同一 Service,而系統(tǒng)也只會(huì)創(chuàng)建一個(gè)對應(yīng) Service 的實(shí)例)。
??
因此你可以把 Service 想象成一種消息服務(wù),而你可以在任何有 Context 的地方調(diào)用 Context.startService、Context.stopService、Context.bindService,Context.unbindService,來控制它,你也可以在 Service 里注冊 BroadcastReceiver,在其他地方通過發(fā)送 broadcast 來控制它,當(dāng)然這些都是 Thread 做不到的。
??
3、Service的生命周期
?
onCreate onStart onDestroy onBind?
1). 被啟動(dòng)的服務(wù)的生命周期:如果一個(gè)Service被某個(gè)Activity 調(diào)用 Context.startService 方法啟動(dòng),那么不管是否有Activity使用bindService綁定或unbindService解除綁定到該Service,該Service都在后臺(tái)運(yùn)行。如果一個(gè)Service被startService 方法多次啟動(dòng),那么onCreate方法只會(huì)調(diào)用一次,onStart將會(huì)被調(diào)用多次(對應(yīng)調(diào)用startService的次數(shù)),并且系統(tǒng)只會(huì)創(chuàng)建Service的一個(gè)實(shí)例(因此你應(yīng)該知道只需要一次stopService調(diào)用)。該Service將會(huì)一直在后臺(tái)運(yùn)行,而不管對應(yīng)程序的Activity是否在運(yùn)行,直到被調(diào)用stopService,或自身的stopSelf方法。當(dāng)然如果系統(tǒng)資源不足,android系統(tǒng)也可能結(jié)束服務(wù)。
?
2). 被綁定的服務(wù)的生命周期:如果一個(gè)Service被某個(gè)Activity 調(diào)用 Context.bindService 方法綁定啟動(dòng),不管調(diào)用 bindService?調(diào)用幾次,onCreate方法都只會(huì)調(diào)用一次,同時(shí)onStart方法始終不會(huì)被調(diào)用。當(dāng)連接建立之后,Service將會(huì)一直運(yùn)行,除非調(diào)用Context.unbindService 斷開連接或者之前調(diào)用bindService?的 Context 不存在了(如Activity被finish的時(shí)候),系統(tǒng)將會(huì)自動(dòng)停止Service,對應(yīng)onDestroy將被調(diào)用。
?
3). 被啟動(dòng)又被綁定的服務(wù)的生命周期:如果一個(gè)Service又被啟動(dòng)又被綁定,則該Service將會(huì)一直在后臺(tái)運(yùn)行。并且不管如何調(diào)用,onCreate始終只會(huì)調(diào)用一次,對應(yīng)startService調(diào)用多少次,Service的onStart便會(huì)調(diào)用多少次。調(diào)用unbindService將不會(huì)停止Service,而必須調(diào)用 stopService 或 Service的 stopSelf 來停止服務(wù)。
?
4). 當(dāng)服務(wù)被停止時(shí)清除服務(wù):當(dāng)一個(gè)Service被終止(1、調(diào)用stopService;2、調(diào)用stopSelf;3、不再有綁定的連接(沒有被啟動(dòng)))時(shí),onDestroy方法將會(huì)被調(diào)用,在這里你應(yīng)當(dāng)做一些清除工作,如停止在Service中創(chuàng)建并運(yùn)行的線程。
?
特別注意:
1、你應(yīng)當(dāng)知道在調(diào)用 bindService 綁定到Service的時(shí)候,你就應(yīng)當(dāng)保證在某處調(diào)用 unbindService 解除綁定(盡管 Activity 被 finish 的時(shí)候綁定會(huì)自 動(dòng)解除,并且Service會(huì)自動(dòng)停止);
2、你應(yīng)當(dāng)注意 使用 startService 啟動(dòng)服務(wù)之后,一定要使用 stopService停止服務(wù),不管你是否使用bindService;?
3、同時(shí)使用 startService 與 bindService 要注意到,Service 的終止,需要unbindService與stopService同時(shí)調(diào)用,才能終止 Service,不管 startService 與 bindService 的調(diào)用順序,如果先調(diào)用 unbindService 此時(shí)服務(wù)不會(huì)自動(dòng)終止,再調(diào)用 stopService 之后服務(wù)才會(huì)停止,如果先調(diào)用 stopService 此時(shí)服務(wù)也不會(huì)終止,而再調(diào)用 unbindService 或者 之前調(diào)用 bindService 的 Context 不存在了(如Activity 被 finish 的時(shí)候)之后服務(wù)才會(huì)自動(dòng)停止;
4、當(dāng)在旋轉(zhuǎn)手機(jī)屏幕的時(shí)候,當(dāng)手機(jī)屏幕在“橫”“豎”變換時(shí),此時(shí)如果你的 Activity 如果會(huì)自動(dòng)旋轉(zhuǎn)的話,旋轉(zhuǎn)其實(shí)是 Activity 的重新創(chuàng)建,因此旋轉(zhuǎn)之前的使用 bindService 建立的連接便會(huì)斷開(Context 不存在了),對應(yīng)服務(wù)的生命周期與上述相同。
5、在 sdk 2.0 及其以后的版本中,對應(yīng)的 onStart 已經(jīng)被否決變?yōu)榱?onStartCommand,不過之前的 onStart 任然有效。這意味著,如果你開發(fā)的應(yīng)用程序用的 sdk 為 2.0 及其以后的版本,那么你應(yīng)當(dāng)使用 onStartCommand 而不是 onStart。
?
4、startService 啟動(dòng)服務(wù)
??
想要用 startService ?啟動(dòng)服務(wù),不管Local 還是 Remote 我們需要做的工作都是一樣簡單。當(dāng)然要記得在 Androidmanifest.xml 中注冊 service。
根據(jù)上面的生命周期,我們便會(huì)給出 Service 中的代碼框架:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | packagecom.newcj.test; importandroid.app.Service; importandroid.content.Intent; importandroid.os.IBinder; publicclassLocalService1extendsService { /** * onBind 是 Service 的虛方法,因此我們不得不實(shí)現(xiàn)它。 * 返回 null,表示客服端不能建立到此服務(wù)的連接。 */ @Override publicIBinder onBind(Intent intent) { returnnull; } @Override publicvoidonCreate() { super.onCreate(); } @Override publicvoidonStart(Intent intent,intstartId) { super.onStart(intent, startId); } @Override publicvoidonDestroy() { super.onDestroy(); } } |
對應(yīng)生命周期系統(tǒng)回調(diào)函數(shù)上面已經(jīng)說明,在對應(yīng)地方加上適當(dāng)?shù)拇a即可。下面是啟動(dòng)與停止 Service 的代碼:
?| 1 2 3 4 5 | // 啟動(dòng)一個(gè) Activity startActivity(newIntent(this, LocalService1.class)); ... // 停止一個(gè) Activity stopService(newIntent(this, LocalService1.class)); |
對應(yīng)的 Intent 為標(biāo)志服務(wù)類的 Intent。
?
5、Local 與 Remote 服務(wù)綁定
?
同樣記得在 Androidmanifest.xml 中注冊 service
1). Local 服務(wù)綁定:Local 服務(wù)的綁定較簡單,首先在 Service 中我們需要實(shí)現(xiàn) Service?的抽象方法 onBind,并返回一個(gè)實(shí)現(xiàn) IBinder 接口的對象。
Service 中的代碼:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | packagecom.newcj.test; importandroid.app.Service; importandroid.content.Intent; importandroid.os.Binder; importandroid.os.IBinder; publicclassLocalServiceextendsService { /** * 在 Local Service 中我們直接繼承 Binder 而不是 IBinder,因?yàn)?Binder 實(shí)現(xiàn)了 IBinder 接口,這樣我們可以少做很多工作。 * @author newcj */ publicclassSimpleBinderextendsBinder{ /** * 獲取 Service 實(shí)例 * @return */ publicLocalService getService(){ returnLocalService.this; } publicintadd(inta,intb){ returna + b; } } publicSimpleBinder sBinder; @Override publicvoidonCreate() { super.onCreate(); // 創(chuàng)建 SimpleBinder sBinder =newSimpleBinder(); } @Override publicIBinder onBind(Intent intent) { // 返回 SimpleBinder 對象 returnsBinder; } } |
上面的代碼關(guān)鍵之處,在于 onBind(Intent) 這個(gè)方法 返回了一個(gè)實(shí)現(xiàn)了 IBinder 接口的對象,這個(gè)對象將用于綁定Service 的?Activity 與 Local?Service 通信。下面是 Activity 中的代碼:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | packagecom.newcj.test; importandroid.app.Activity; importandroid.content.ComponentName; importandroid.content.Context; importandroid.content.Intent; importandroid.content.ServiceConnection; importandroid.os.Bundle; importandroid.os.IBinder; importandroid.util.Log; importandroid.view.View; importandroid.view.View.OnClickListener; publicclassMainextendsActivity { privatefinalstaticString TAG ="SERVICE_TEST"; privateServiceConnection sc; privatebooleanisBind; @Override publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sc =newServiceConnection() { @Override publicvoidonServiceDisconnected(ComponentName name) { } @Override publicvoidonServiceConnected(ComponentName name, IBinder service) { LocalService.SimpleBinder sBinder = (LocalService.SimpleBinder)service; Log.v(TAG,"3 + 5 = "+ sBinder.add(3,5)); Log.v(TAG, sBinder.getService().toString()); } }; findViewById(R.id.btnBind).setOnClickListener(newOnClickListener() { @Override publicvoidonClick(View v) { bindService(newIntent(Main.this, LocalService.class), sc, Context.BIND_AUTO_CREATE); isBind =true; } }); findViewById(R.id.btnUnbind).setOnClickListener(newOnClickListener() { @Override publicvoidonClick(View v) { if(isBind){ unbindService(sc); isBind =false; } } }); } } |
在 Activity 中,我們通過 ServiceConnection 接口來取得建立連接 與 連接意外丟失的回調(diào)。bindService有三個(gè)參數(shù),第一個(gè)是用于區(qū)分 Service 的Intent 與 startService 中的 Intent 一致,第二個(gè)是實(shí)現(xiàn)了 ServiceConnection 接口的對象,最后一個(gè)是 flag 標(biāo)志位。有兩個(gè)flag,BIND_DEBUG_UNBIND 與 BIND_AUTO_CREATE,前者用于調(diào)試(詳細(xì)內(nèi)容可以查看javadoc 上面描述的很清楚),后者默認(rèn)使用。unbindService 解除綁定,參數(shù)則為之前創(chuàng)建的 ServiceConnection 接口對象。另外,多次調(diào)用 unbindService 來釋放相同的連接會(huì)拋出異常,因此我創(chuàng)建了一個(gè) boolean 變量來判斷是否 unbindService 已經(jīng)被調(diào)用過。
運(yùn)行結(jié)果:
?
2). Remote 服務(wù)綁定:Remote 的服務(wù)綁定由于服務(wù)是在另外一個(gè)進(jìn)程,因此需要用到 android 的 IPC 機(jī)制。這將又是一個(gè)很長的話題,因此,我打算寫另外一篇 android 的 IPC 機(jī)制分析 ,并在其中進(jìn)行詳述,然后在這里更新鏈接,敬請關(guān)注。
?
特別注意:
1、Service.onBind如果返回null,則調(diào)用 bindService 會(huì)啟動(dòng) Service,但不會(huì)連接上 Service,因此?ServiceConnection.onServiceConnected 不會(huì)被調(diào)用,但你任然需要使用 unbindService 函數(shù)斷開它,這樣 Service?才會(huì)停止。
?
?6、創(chuàng)建前臺(tái)服務(wù)
??
前臺(tái)服務(wù)的優(yōu)點(diǎn)上面已經(jīng)說明,但設(shè)置服務(wù)為前臺(tái)服務(wù),我們需要注意在 sdk 2.0 及其以后版本使用的方法是 startForeground 與 stopForeground,之前版本使用的是 setForeground ,因此如果你應(yīng)用程序的最低運(yùn)行環(huán)境要求是 2.0,那么這里可以直接運(yùn)用新方法,如果運(yùn)行環(huán)境是2.0以下,那么為了保證向后兼容性,這里必須使用反射技術(shù)來調(diào)用新方法。
下面是我仿照 ApiDemos 重新敲的代碼,對某些地方進(jìn)行了修改,因此更具有說明性:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | packagecom.newcj.test; importjava.lang.reflect.InvocationTargetException; importjava.lang.reflect.Method; importandroid.app.Notification; importandroid.app.NotificationManager; importandroid.app.PendingIntent; importandroid.app.Service; importandroid.content.Context; importandroid.content.Intent; importandroid.os.IBinder; publicclassForegroundServiceextendsService { privatestaticfinalClass[] mStartForegroundSignature =newClass[] { int.class, Notification.class}; privatestaticfinalClass[] mStopForegroundSignature =newClass[] { boolean.class}; privateNotificationManager mNM; privateMethod mStartForeground; privateMethod mStopForeground; privateObject[] mStartForegroundArgs =newObject[2]; privateObject[] mStopForegroundArgs =newObject[1]; @Override publicIBinder onBind(Intent intent) { returnnull; } @Override publicvoidonCreate() { super.onCreate(); mNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); try{ mStartForeground = ForegroundService.class.getMethod("startForeground", mStartForegroundSignature); mStopForeground = ForegroundService.class.getMethod("stopForeground", mStopForegroundSignature); }catch(NoSuchMethodException e) { mStartForeground = mStopForeground =null; } // 我們并不需要為 notification.flags 設(shè)置 FLAG_ONGOING_EVENT,因?yàn)? // 前臺(tái)服務(wù)的 notification.flags 總是默認(rèn)包含了那個(gè)標(biāo)志位 Notification notification =newNotification(R.drawable.icon,"Foreground Service Started.", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this,0, newIntent(this, Main.class),0); notification.setLatestEventInfo(this,"Foreground Service", "Foreground Service Started.", contentIntent); // 注意使用? startForeground ,id 為 0 將不會(huì)顯示 notification startForegroundCompat(1, notification); } @Override publicvoidonDestroy() { super.onDestroy(); stopForegroundCompat(1); } // 以兼容性方式開始前臺(tái)服務(wù) privatevoidstartForegroundCompat(intid, Notification n){ if(mStartForeground !=null){ mStartForegroundArgs[0] = id; mStartForegroundArgs[1] = n; try{ mStartForeground.invoke(this, mStartForegroundArgs); }catch(IllegalArgumentException e) { e.printStackTrace(); }catch(IllegalAccessException e) { e.printStackTrace(); }catch(InvocationTargetException e) { e.printStackTrace(); } return; } setForeground(true); mNM.notify(id, n); } // 以兼容性方式停止前臺(tái)服務(wù) privatevoidstopForegroundCompat(intid){ if(mStopForeground !=null){ mStopForegroundArgs[0] = Boolean.TRUE; try{ mStopForeground.invoke(this, mStopForegroundArgs); }catch(IllegalArgumentException e) { e.printStackTrace(); }catch(IllegalAccessException e) { e.printStackTrace(); }catch(InvocationTargetException e) { e.printStackTrace(); } return; } //? 在 setForeground 之前調(diào)用 cancel,因?yàn)槲覀冇锌赡茉谌∠芭_(tái)服務(wù)之后 //? 的那一瞬間被kill掉。這個(gè)時(shí)候 notification 便永遠(yuǎn)不會(huì)從通知一欄移除 mNM.cancel(id); setForeground(false); } } |
特別注意:
??1、使用 startForeground ,如果 id 為 0 ,那么 notification 將不會(huì)顯示。
??
7、在什么情況下使用 startService?或 bindService 或 同時(shí)使用startService 和 bindService
??
如果你只是想要啟動(dòng)一個(gè)后臺(tái)服務(wù)長期進(jìn)行某項(xiàng)任務(wù)那么使用 startService 便可以了。如果你想要與正在運(yùn)行的 Service 取得聯(lián)系,那么有兩種方法,一種是使用 broadcast ,另外是使用 bindService ,前者的缺點(diǎn)是如果交流較為頻繁,容易造成性能上的問題,并且 BroadcastReceiver 本身執(zhí)行代碼的時(shí)間是很短的(也許執(zhí)行到一半,后面的代碼便不會(huì)執(zhí)行),而后者則沒有這些問題,因此我們肯定選擇使用 bindService(這個(gè)時(shí)候你便同時(shí)在使用 startService 和 bindService 了,這在 Activity 中更新 Service 的某些運(yùn)行狀態(tài)是相當(dāng)有用的)。另外如果你的服務(wù)只是公開一個(gè)遠(yuǎn)程接口,供連接上的客服端(android 的 Service 是C/S架構(gòu))遠(yuǎn)程調(diào)用執(zhí)行方法。這個(gè)時(shí)候你可以不讓服務(wù)一開始就運(yùn)行,而只用 bindService ,這樣在第一次 bindService 的時(shí)候才會(huì)創(chuàng)建服務(wù)的實(shí)例運(yùn)行它,這會(huì)節(jié)約很多系統(tǒng)資源,特別是如果你的服務(wù)是Remote Service,那么該效果會(huì)越明顯(當(dāng)然在 Service 創(chuàng)建的時(shí)候會(huì)花去一定時(shí)間,你應(yīng)當(dāng)注意到這點(diǎn))。
??
?
8、在 AndroidManifest.xml?里 Service 元素的常見選項(xiàng)
android:name ------------- 服務(wù)類名
android:label -------------- 服務(wù)的名字,如果此項(xiàng)不設(shè)置,那么默認(rèn)顯示的服務(wù)名則為類名
android:icon -------------- 服務(wù)的圖標(biāo)
android:permission ------- 申明此服務(wù)的權(quán)限,這意味著只有提供了該權(quán)限的應(yīng)用才能控制或連接此服務(wù)
android:process ---------- 表示該服務(wù)是否運(yùn)行在另外一個(gè)進(jìn)程,如果設(shè)置了此項(xiàng),那么將會(huì)在包名后面加上這段字符串表示另一進(jìn)程的名字
android:enabled ---------- 如果此項(xiàng)設(shè)置為 true,那么 Service 將會(huì)默認(rèn)被系統(tǒng)啟動(dòng),不設(shè)置默認(rèn)此項(xiàng)為 false
android:exported --------- 表示該服務(wù)是否能夠被其他應(yīng)用程序所控制或連接,不設(shè)置默認(rèn)此項(xiàng)為 false
轉(zhuǎn)載于:https://my.oschina.net/javaTechLover/blog/91350
總結(jié)
以上是生活随笔為你收集整理的Android之Service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [linux学习] 字符界面linux安
- 下一篇: 线上lnmp环境快速安装