這個(gè)demo的功能很簡單,就是輸出一句log.這篇文章只是講了下添加系統(tǒng)服務(wù)的具體操作,沒有講原理,后面不出意外的話應(yīng)該會(huì)寫一篇文章專門講這個(gè),希望不會(huì)鴿.
首先說明添加系統(tǒng)服務(wù)一共分為兩步: 1.自定義系統(tǒng)服務(wù)(HelloService), 2. 注冊系統(tǒng)服務(wù).
1. 自定義系統(tǒng)服務(wù)HelloService
文件位置:frameworks/base/core/java/android/app
- 添加 HelloServiceManager.java
- 添加 IHelloServiceManager.aidl (可以不加,主要是為了生成3)
- 添加 IHelloServiceManager.java( aidl + 2 自動(dòng)生成)
- 添加 HelloService.java(文件位置frameworks/base/services/core/java/com/android/server/)
HelloServiceManager.java
package android.app;
import android.os.RemoteException;
import android.annotation.SystemService;
import android.content.Context;
@SystemService(Context.HELLO_SERVICE)
public final class HelloServiceManager{
private final IHelloServiceManager mService;
private Context mContext;
/**
* @hide to prevent subclassing from outside of the framework
*/
HelloServiceManager(Context context,IHelloServiceManager service){
mContext = context;
mService = service;
}
public void printHello(){
try{
mService.printHello();
}catch (RemoteException ex){
}
}
IHelloServiceManager.aidl (這個(gè)可以利用AS自動(dòng)生成IHelloServiceManager.java,然后直接添加java文件就行,不用再添加aidl)
package android.app;
/**
* System private API for talking with the helloworld service.
* {@hide}
*/
interface IHelloServiceManager{
void printHello();
}
HelloService.java(注意該文件添加位置與前兩個(gè)不同).
package com.android.server;
import android.app.IHelloWorldManager;
import android.content.Context;
import android.os.RemoteException;
import android.util.Slog;
import android.content.Context;
import com.android.server.SystemService;
public class HelloService extends IHelloServiceManager.Stub {
private final static String LOG_TAG = "HelloService";
private final Object mLock = new Object();
private final Context mContext;
HelloService(Context context) {
mContext = context;
}
@Override
public void printHello() throws RemoteException {
Slog.i(LOG_TAG,"Hello,World!");
}
}
一個(gè)自定義的service就完成了.
2. 注冊Service
修改:
frameworks/base/core/java/android/app/SystemServiceRegistry.java
frameworks/base/core/java/android/content/Context.java
frameworks/base/services/java/com/android/server/SystemServer.java
SystemServiceRegistry.java //以下代碼放在static塊即可
registerService(Context.HELLO_SERVICE, HelloServiceManager.class,
new CachedServiceFetcher<HelloServiceManager>() {
@Override
public HelloServiceManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder binder;
if (true){//ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
binder = ServiceManager.getServiceOrThrow(Context.HELLO_SERVICE);
} else {
binder = ServiceManager.getService(Context.HELLO_SERVICE);
}
return new HelloServiceManager(ctx,IHelloServiceManager.Stub.asInterface(binder));
}});
Context.java(仿照其他service添加)
主要有兩處,一處定義HELLO_SERVICE, 另一處StringDef塊里.
ACCOUNT_SERVICE,
ACTIVITY_SERVICE,
ALARM_SERVICE,
+ HELLO_SERVICE,
NOTIFICATION_SERVICE,
ACCESSIBILITY_SERVICE,
CAPTIONING_SERVICE,
...
public static final String HELLO_SERVICE = "hello";
SystemServer.java
在startBootstrapServices()函數(shù)中添加,啟動(dòng)最早,也可以在startCoreServices()或者startOtherServices()中添加.
private static final String HELLO_SERVICE = "com.android.server.HelloService";
...
//startBootstrapServices()函數(shù)中添加
if(true){
HelloService helloservice = null;
try{
traceBeginAndSlog("HelloService");
helloservice = new HelloService(mSystemContext);
ServiceManager.addService(Context.HELLO_SERVICE,helloservice);
}catch(Throwable e){
Slog.e(TAG, "Starting HelloService failed!!! ", e);
}
traceEnd();
}
以上就是添加系統(tǒng)服務(wù)的全部代碼.
3 編譯源碼 及 驗(yàn)證
編譯只需要兩個(gè)命令.(在source build/envsetup.sh 以及 lunch 后) 首先make update-api 然后再make即可.
寫一個(gè)app然后調(diào)用
HelloService hs = (HelloService) getSystemService(Context.HELLO_SERVICE);
hs.printhello();
如果不出意外的話AS會(huì)報(bào)錯(cuò),因?yàn)镾DK里面沒有HelloService 和 Context.HELLO_SERVICE這寫東西. 但其實(shí)系統(tǒng)是有的,只是編譯器不知道罷了, 所以騙過AS就好, 把上面的兩行代碼用反射實(shí)驗(yàn)就好.
最后 點(diǎn)個(gè)贊吧. 關(guān)注下就更好了. 謝謝!
參考文章
android添加service