Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法詳解
本文實(shí)例講述了Android實(shí)現(xiàn)在ServiceManager中加入自定義服務(wù)的方法。分享給大家供大家參考,具體如下:
當(dāng)我們要使用android的系統(tǒng)服務(wù)時(shí),一般都是使用Context.getSystemService
方法。例如我們要獲取AudioManager,我們可以:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
獲取的服務(wù),其實(shí)是在ServiceManager中注冊(cè)的Binder服務(wù),然后進(jìn)行封裝后,提供給用戶。
可以看ContextImpl.java中的實(shí)現(xiàn):
static { ...... // 將AudioManager加入SYSTEM_SERVICE_MAP中,調(diào)用getSystemService時(shí), // 就會(huì)從SYSTEM_SERVICE_MAP得到AudioManager registerService(AUDIO_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new AudioManager(ctx); }}); ...... }
AudioManager是對(duì)IAudioService的封裝,實(shí)際操作都是使用IAudioService進(jìn)行的,看AudioManager中的代碼:
private static IAudioService getService() { if (sService != null) { return sService; } // 從ServiceManager中獲取Binder IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); // 將Binder轉(zhuǎn)化成IAudioService,方便調(diào)用 sService = IAudioService.Stub.asInterface(b); return sService; }
上面是android系統(tǒng)的使用方式。如果我們添加自己的服務(wù),要如何做呢?
我們?cè)趀clipse中建3個(gè)測(cè)試工程:
1)MyServiceLib:這是個(gè)lib工程,需要在eclipse中勾選Is Library。后面的兩個(gè)工程,都需要將MyServiceLib添加到Library中。
2) MyService: 用于在android開機(jī)時(shí)注冊(cè)自定義服務(wù)進(jìn)ServiceManager。因?yàn)镾erviceManager被@hide隱藏了,所以要使用它需要自己手動(dòng)添加sdk包,添加方式可參考在Eclipse中使用SDK中@hide函數(shù)的方法附加說(shuō)明。另外,添加服務(wù),需要System用戶,所以manifest文件中需要加上android:sharedUserId="android.uid.system", 并且要使用platform簽名簽名apk。
3)MyServiceTest:用于測(cè)試上面兩個(gè)工程。
下面我們就來(lái)編碼。
先在MyServiceLib工程中創(chuàng)建一個(gè)aidl文件,android編譯工具會(huì)幫我們生成相應(yīng)的java類,aidl文件如下
package com.test.lib; interface IMyService { void setValue(int val); int getValue(); }
定義了兩個(gè)接口用于測(cè)試,setValue和getValue。
android編譯工具會(huì)幫我們?cè)趃en目錄下生成一個(gè)IMyService的java類。
2. 在MyService工程中創(chuàng)建MyService類, 這個(gè)類繼承自IMyService.Stub,實(shí)現(xiàn)了setValue和getValue接口,這就是一個(gè)Service。
package com.test.myservice; import android.os.RemoteException; import com.test.lib.IMyService; public class MyService extends IMyService.Stub { private int value; @Override public void setValue(int val) throws RemoteException { this.value = val; } @Override public int getValue() throws RemoteException { return value; } }
下面我們將把它加入至ServiceManager中。
3. 在MyService工程中創(chuàng)建MyServiceApplication類
package com.test.myservice; import android.app.Application; import android.os.ServiceManager; public class MyServiceApplication extends Application{ @Override public void onCreate() { super.onCreate(); ServiceManager.addService("MYSERVICE", new MyService()); } }
這是一個(gè)Application,我們希望android系統(tǒng)啟動(dòng)時(shí),就創(chuàng)建這個(gè)Application,在onCreate方法中,創(chuàng)建MyService類,并加入到ServiceManager中。因此,我需要修改下manifest文件
<application android:name=".MyServiceApplication" //指定Application為我們創(chuàng)建的MyServiceApplication android:allowBackup="true" android:icon="@drawable/ic_launcher" android:persistent="true" // 加上persistent=ture,ActivityManager創(chuàng)建的時(shí)候,就會(huì)創(chuàng)建該應(yīng)用的進(jìn)程,并調(diào)用MyServiceApplication的onCreate方法 android:label="@string/app_name" android:theme="@style/AppTheme" >
注意,這個(gè)應(yīng)用需要system用戶,并簽名才可運(yùn)行。
這樣,服務(wù)端就好了,并且開機(jī)時(shí),我們的服務(wù)就已經(jīng)在ServiceManager中了。
4. 下面我們提供一個(gè)Manager類方便客戶端使用。在MyServiceLib中創(chuàng)建MyManager類:
package com.test.lib; import android.os.RemoteException; import android.os.ServiceManager; public class MyManager { private static MyManager instance; private IMyService myservice; public static MyManager getInstance() { if (instance == null) { instance = new MyManager(); } return instance; } private MyManager() { // 從ServiceManager中獲取服務(wù) myservice = IMyService.Stub.asInterface(ServiceManager.getService("MYSERVICE")); } public void setValue(int value) throws RemoteException { myservice.setValue(value); } public int getValue() throws RemoteException { return myservice.getValue(); } }
5. 在MyServiceTest工程中進(jìn)行測(cè)試
通過(guò)MyManager.getInstance()
可以很方便的獲取服務(wù)的Manager,對(duì)遠(yuǎn)程服務(wù)進(jìn)行調(diào)用。我們創(chuàng)建一個(gè)Activity來(lái)使用MyManager
package com.test.client; import java.util.Random; import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.os.Bundle; import android.os.RemoteException; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.test.binder.client.R; import com.test.lib.MyManager; public class MainActivity extends Activity implements OnClickListener { MyManager myManager; Button btnSetValue; Button btnGetValue; TextView tvValue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); setContentView(R.layout.activity_main); btnSetValue = (Button) findViewById(R.id.btn_set_value); btnGetValue = (Button) findViewById(R.id.btn_get_value); tvValue = (TextView) findViewById(R.id.tv_value); // 獲取MyManager myManager = MyManager.getInstance(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_set_value: int value = new Random().nextInt(); try { myManager.setValue(value); Toast.makeText(this, "set value to "+value+ " success!", 0).show(); } catch (RemoteException e) { e.printStackTrace(); Toast.makeText(this, "set value fail!", 0).show(); } break; case R.id.btn_get_value: try { tvValue.setText("value:"+myManager.getValue()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } } }
附:在Eclipse中使用SDK中@hide函數(shù)的方法
我們使用Eclipse進(jìn)行android開發(fā)時(shí),使用的是ADT中提供的SDK,里面是不包含@hide函數(shù)和變量的。因?yàn)閍ndroid為了兼容、安全等原因,在提供SDK時(shí),把這些函數(shù)給隱藏了。但是,很多時(shí)候,我們又需要使用這些函數(shù),因此我們需要手動(dòng)添加android SDK。例如,當(dāng)我們使用AudioManager時(shí),當(dāng)需要看某種streamType是否mute時(shí),可以調(diào)用isStreamMute(int streamType)
這個(gè)方法,但是因?yàn)樗茾hide的,所以我們就需要引入自己的sdk,才能編譯通過(guò)。
1. android系統(tǒng)編譯時(shí),當(dāng)編譯“include $(BUILD_JAVA_LIBRARY)”時(shí),會(huì)在$ANDROID_SOURCE_BASE/out/target/common/obj/JAVA_LIBRARIES生成中間文件,當(dāng)我們需要使用某些類庫(kù)時(shí),可以從這里面找。
isStreamMute(int streamType)在framework.jar中,我們從out/target/common/obj/JAVA_LIBRARIES/framework_intermediates中,將classes.jar拷貝到本地,并重命名為framework.jar。
2. 在eclipse中右鍵工程->Properties->Java Build Path->Libraries->Add External JAR
3. 點(diǎn)擊Order and Export,將framework.jar 置頂
4. 現(xiàn)在,我們就可以使用AudioManager中的isStreamMute(int streamType)方法了
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android資源操作技巧匯總》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能
- Android 系統(tǒng)服務(wù)TelecomService啟動(dòng)過(guò)程原理分析
- Android8.0適配前臺(tái)定位服務(wù)service的示例代碼
- 淺談Android Service服務(wù)的高級(jí)技巧
- 說(shuō)說(shuō)在Android如何使用服務(wù)(Service)的方法
- Android7.0指紋服務(wù)FingerprintService實(shí)例介紹
- Android實(shí)現(xiàn)Service在前臺(tái)運(yùn)行服務(wù)
- Android服務(wù)Service教程
相關(guān)文章
Android仿小米安全中心檢測(cè)進(jìn)度條效果
這篇文章主要介紹了Android仿小米安全中心檢測(cè)進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android Studio快捷鍵生成TAG、Log.x日志輸出介紹
這篇文章主要介紹了Android Studio快捷鍵生成TAG、Log.x日志輸出介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法
今天小編就為大家分享一篇Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Flutter Reusable Lottie Animations技巧
這篇文章主要為大家介紹了Flutter Reusable Lottie Animations技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android實(shí)現(xiàn)圖片輪播效果的兩種方法
android圖片輪播效果非常漂亮,在程序開發(fā)中也經(jīng)常用到,本文給大家分享android實(shí)現(xiàn)圖片輪播效果的幾種方法,對(duì)android實(shí)現(xiàn)圖片輪播相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android自定義ScrollView實(shí)現(xiàn)放大回彈效果實(shí)例代碼
本篇文章主要介紹了Android自定義ScrollView實(shí)現(xiàn)放大回彈效果實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03Android Socket通信實(shí)現(xiàn)簡(jiǎn)單聊天室
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)編程之Socket通信實(shí)現(xiàn)簡(jiǎn)單聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03