您的位置:首页 > 移动开发 > Android开发

Android-Adding SystemService

2014-07-30 17:42 232 查看
Thiswikipagewilldemonstrate-"Howtoaddsystemservicetoandroidframework".Example-"AddingaBluetoothHIDservice"-takenasreferenceofunderstanding.Thiswillalsohelptoaddsupportformorebluetoothprofilesintoandroidframework.

Contents

1Whatisservice?

2Servicelayer

3Createservice

4Registerservice

5Exposeservice

6Add[service].aidlforbuild

7Usingservice

8References

9Support

Whatisservice?

Asperthedefinitiongivenathttp://developer.android.com/guide/topics/fundamentals/services.html

AServiceisanapplicationcomponentthatcanperformlong-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.aidlfileatfollowinglocation

frameworks/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

Touseservice

firstgetservicehandleusing"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

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: