android添加系统服务

来源:互联网 发布:济南软件开发的 编辑:程序博客网 时间:2024/06/06 00:57

1 添加fake.aidl文件

package android.app;interface IFakeManager{    int getValue();    void setValue(int value);}

在frameworks/base/Android.mk文件中的LOCAL_SRC_FILES增加aidl的声明

@@ -63,6 +63,7 @@ LOCAL_SRC_FILES += \
        core/java/android/accounts/IAccountManagerResponse.aidl \
        core/java/android/accounts/IAccountAuthenticator.aidl \
        core/java/android/accounts/IAccountAuthenticatorResponse.aidl \
+       core/java/android/app/IFakeManager.aidl \
        core/java/android/app/IActivityContainer.aidl \
        core/java/android/app/IActivityContainerCallback.aidl \
        core/java/android/app/IActivityController.aidl \

3 添加FakeManager.java

package android.app;import android.os.Handler;import android.os.RemoteException;import android.util.Log;public class FakeManager{        public static final String TAG = "FakeManager";        IFakeManager mService;        Handler mHandler;        public FakeManager(IFakeManager service, Handler handler) {                mService = service;                mHandler = handler;        }        public int getValue(){            try {                return mService.getValue();            } catch (RemoteException e) {                Log.e(TAG, "[getValue] RemoteException");            }            return -1;        }        public void setValue(int value) {            try {                mService.setValue(value);            } catch (RemoteException e) {                Log.e(TAG, "[setValue] RemoteException");            }        }}

4 添加服务java文件FakeService.java

package com.android.server;import android.app.IFakeManager;import android.util.Log;import android.content.Context;public class FakeService extends IFakeManager.Stub{    private static final String TAG = "FakeService";    private Context mContext;    int value;    public FakeService(Context context){        mContext = context;    }    public int getValue() {        Log.d(TAG,"[getValue] name : "+value);        return this.value;    }    public void setValue(int value) {        Log.d(TAG,"[setValue] "+ value);        this.value = value;    }}

5 更改frameworks/base/services/java/com/android/server/SystemServer.java

@@ -1061,6 +1061,14 @@ public final class SystemServer {
         } catch (Throwable e) {
             reportWtf("making Display Manager Service ready", e);
         }
+        
+        try {  
+            Slog.i(TAG, "FakeManager Service");  
+            FakeService fakeService =  new FakeService(context);  
+            ServiceManager.addService(Context.FAKE_SERVICE, fakeService);  
+        } catch (Throwable e) {  
+            reportWtf("starting devInfo Service", e);  
+        }  


6 更改frameworks/base/core/java/android/app/ContextImpl.java

@@ -768,6 +768,14 @@ class ContextImpl extends Context {
                 IBinder b = ServiceManager.getService(APPWIDGET_SERVICE);
                 return new AppWidgetManager(ctx, IAppWidgetService.Stub.asInterface(b));
             }});
+        
+        registerService(FAKE_SERVICE, new ServiceFetcher() {
+            public Object createService(ContextImpl ctx) {
+                    IBinder b = ServiceManager.getService(FAKE_SERVICE);
+                    IFakeManager service = IFakeManager.Stub.asInterface(b);
+                    return new FakeManager(service, ctx.mMainThread.getHandler());
+            }
+    });
     }


7 更改frameworks/base/core/java/android/content/Context.java

@@ -67,6 +67,7 @@ import java.lang.annotation.RetentionPolicy;
  * broadcasting and receiving intents, etc.
  */
 public abstract class Context {
+    public static final String FAKE_SERVICE = "fake_service"; 
     /**
      * File creation mode: the default mode, where the created file can only
      * be accessed by the calling application (or all applications sharing the


0 0