Android-Adding SystemService
2014-07-30 17:42
232 查看
Thiswikipagewilldemonstrate-"Howtoaddsystemservicetoandroidframework".Example-"AddingaBluetoothHIDservice"-takenasreferenceofunderstanding.Thiswillalsohelptoaddsupportformorebluetoothprofilesintoandroidframework.
1Whatisservice?
2Servicelayer
3Createservice
4Registerservice
5Exposeservice
6Add[service].aidlforbuild
7Usingservice
8References
9Support
http://developer.android.com/guide/topics/fundamentals/services.html
AServiceisanapplicationcomponentthatcanperformlong-runningoperationsinthebackgroundanddoesnotprovideauserinterface.Anotherapplicationcomponentcanstartaserviceanditwillcontinuetoruninthebackgroundeveniftheuserswitchestoanotherapplication.Additionally,acomponentcanbindtoaservicetointeractwithitandevenperforminterprocesscommunication(IPC).Forexample,aservicemighthandlenetworktransactions,playmusic,performfileI/O,orinteractwithacontentprovider,allfromthebackground.
frameworks/base/core/java/android/os/[server].aidl
Rebuildtheframework/baseorandroidsystem.Serviceisnowreadytousebyotherapplication/process.
firstgetservicehandleusing"ServiceManager.getService()"api
useservicehandletocallsetoffunctionsexposedbyservice
Belowisthesamplecodetouseservice.
http://developer.android.com/reference/android/app/Service.html
http://developer.android.com/guide/topics/fundamentals/services.html
http://www.opersys.com/blog/esc-india-2011-wrapup
(from:http://processors.wiki.ti.com/index.php/Android-Adding_SystemService)
Contents
Whatisservice?
AsperthedefinitiongivenatAServiceisanapplicationcomponentthatcanperformlong-runningoperationsinthebackgroundanddoesnotprovideauserinterface.Anotherapplicationcomponentcanstartaserviceanditwillcontinuetoruninthebackgroundeveniftheuserswitchestoanotherapplication.Additionally,acomponentcanbindtoaservicetointeractwithitandevenperforminterprocesscommunication(IPC).Forexample,aservicemighthandlenetworktransactions,playmusic,performfileI/O,orinteractwithacontentprovider,allfromthebackground.
Servicelayer
Createservice
Addyourcodetoframeworks/base/services/java/com/android/server//*TestService.java*/
packagecom.android.server;
importandroid.content.Context;
importandroid.os.Handler;
importandroid.os.ITestService;
importandroid.os.Looper;
importandroid.os.Message;
importandroid.os.Process;
importandroid.util.Log;
publicclassTestServiceextendsITestService.Stub{
privatestaticfinalStringTAG="TestService";
privateTestWorkerThreadmWorker;
privateTestWorkerHandlermHandler;
privateContextmContext;
publicTestService(Contextcontext){
super();
mContext=context;
mWorker=newTestWorkerThread("TestServiceWorker");
mWorker.start();
Log.i(TAG,"Spawnedworkerthread");
}
publicvoidsetValue(intval){
Log.i(TAG,"setValue"+val);
Messagemsg=Message.obtain();
msg.what=TestWorkerHandler.MESSAGE_SET;
msg.arg1=val;
mHandler.sendMessage(msg);
}
privateclassTestWorkerThreadextendsThread{
publicTestWorkerThread(Stringname){
super(name);
}
publicvoidrun(){
Looper.prepare();
mHandler=newTestWorkerHandler();
Looper.loop();
}
}
privateclassTestWorkerHandlerextendsHandler{
privatestaticfinalintMESSAGE_SET=0;
@Override
publicvoidhandleMessage(Messagemsg){
try{
if(msg.what==MESSAGE_SET){
Log.i(TAG,"setmessagereceived:"+msg.arg1);
}
}catch(Exceptione){
//Log,don'tcrash!
Log.e(TAG,"ExceptioninTestWorkerHandler.handleMessage:",e);
}
}
}
}
Registerservice
RegisterserviceinSystemServer.java/*
*gotofunction"@Overridepublicvoidrun()"
*........
*Addfollowingblockafterline"if(factoryTest!=SystemServer.FACTORY_TEST_LOW_LEVEL){"
*/
try{
Slog.i(TAG,"TestService");
ServiceManager.addService(“Test”,newTestService(context));
}catch(Throwablee){
Slog.e(TAG,"FailurestartingTestServiceService",e);
}
Exposeservice
Aservicecanexposesetoffunctionsthatcanbeaccessbyotherprocess/application.Exposedfunctionsarerequiredtobedeclaredin.aidlfileatfollowinglocationframeworks/base/core/java/android/os/[server].aidl
/*
*aidlfile:frameworks/base/core/java/android/os/ITestService.aidl
*Thisfilecontainsdefinitionsoffunctionswhichareexposedbyservice
*/
packageandroid.os;
interfaceITestService{
/**
*{@hide}
*/
voidsetValue(intval);
}
Add[service].aidlforbuild
/*
*openframeworks/base/Android.mkandaddfollowingline
*/
...
core/java/android/os/IPowerManager.aidl\
core/java/android/os/ITestService.aidl\
core/java/android/os/IRemoteCallback.aidl\
...
Rebuildtheframework/baseorandroidsystem.Serviceisnowreadytousebyotherapplication/process.
Usingservice
Touseservicefirstgetservicehandleusing"ServiceManager.getService()"api
useservicehandletocallsetoffunctionsexposedbyservice
Belowisthesamplecodetouseservice.
/*
*HelloServer.java
*/
packagecom.Test.helloserver;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.ServiceManager;
importandroid.os.ITestService;
importandroid.util.Log;
publicclassHelloServerextendsActivity{
privatestaticfinalStringDTAG="HelloServer";
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ITestServiceom=ITestService.Stub.asInterface(ServiceManager.getService("Test"));
try{
Log.d(DTAG,"Goingtocallservice");
om.setValue(20);
Log.d(DTAG,"Servicecalledsuccesfully");
}
catch(Exceptione){
Log.d(DTAG,"FAILEDtocallservice");
e.printStackTrace();
}
}
}
References
(from:http://processors.wiki.ti.com/index.php/Android-Adding_SystemService)
相关文章推荐
- Android-Adding SystemService
- Android-Adding SystemService
- 【Quote】 Android-Adding SystemService
- android中有这么些个SystemService,挨个儿了解
- Android之getSystemService 各种系统服务总结
- Android: Service中创建窗口显示(TYPE_SYSTEM_ALERT, TYPE_SYSTEM_OVERLAY)
- Android getSystemService
- getSystemService()是Android很重要的一个API
- Android之getSystemService方法实现详解
- Android之getSystemService
- Android之getSystemService()方法
- Android 的 system property service
- Android 的 system property service
- Androidの系统服务之getSystemService
- Android getSystemService()
- Android下getService和getSystemService
- Accessing hidden System Service APIs in Android
- Android Audio System 之三: AudioPolicyService 和 AudioPolicyManager
- Android Audio System 之三: AudioPolicyService 和 AudioPolicyManager
- android getSystemService