Android系统开发之一:添加系统级服务方法

说明:本次说的系统服务,是指捆绑在system_process进程中的服务。
仔细阅读源码可以发现,在frameworks/base/services/java/com/android/server/SystemServer.java中添加了很多服务,什么熵信息、电量、wife等等服务,这些服务启动都在launcher之前,一般要继承Binder或者继承一个AIDL。下面试着来添加一个简单系统服务。

http://blog.csdn.net/aiai373824745/article/details/7439896

 

转自:http://blog.csdn.net/pierce0young/article/details/24424991

在android源码中增加一项系统服务,如在android.app包下增加DevInfoManager.java,按如下进行处理:

1、在frameworks/base/core/java/android/app/目录下增加DevInfoManager.java及IDevInfoManager.aidl文件

DevInfoManager.java如下:

[java] view plain copy print ?
  1. package android.app;  
  2. import android.os.Handler;  
  3. import android.os.RemoteException;  
  4. import android.util.Log;  
  5. public class DevInfoManager  
  6. {  
  7.         public static final String TAG = "DevInfoManager";  
  8.         IDevInfoManager mService;  
  9.         Handler mHandler;  
  10.   
  11.         public DevInfoManager(IDevInfoManager service, Handler handler) {  
  12.                 mService = service;  
  13.                 mHandler = handler;  
  14.         }  
  15.   
  16.         public String getValue(String name){  
  17.             try {  
  18.                 return mService.getValue(name);  
  19.             } catch (RemoteException e) {  
  20.                 Log.e(TAG, "[getValue] RemoteException");  
  21.             }  
  22.             return null;  
  23.         }  
  24.   
  25.         public int update(String name, String value, int attribute) {  
  26.             try {  
  27.                 return mService.update(name, value, attribute);  
  28.             } catch (RemoteException e) {  
  29.                 Log.e(TAG, "[update] RemoteException");  
  30.             }  
  31.             return -1;  
  32.         }  
  33. }  
package android.app;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
public class DevInfoManager
{
        public static final String TAG = "DevInfoManager";
        IDevInfoManager mService;
        Handler mHandler;

        public DevInfoManager(IDevInfoManager service, Handler handler) {
                mService = service;
                mHandler = handler;
        }

        public String getValue(String name){
            try {
                return mService.getValue(name);
            } catch (RemoteException e) {
                Log.e(TAG, "[getValue] RemoteException");
            }
            return null;
        }

        public int update(String name, String value, int attribute) {
            try {
                return mService.update(name, value, attribute);
            } catch (RemoteException e) {
                Log.e(TAG, "[update] RemoteException");
            }
            return -1;
        }
}

IDevInfoManager.aidl文件如下:

[java] view plain copy print ?
  1. package android.app;  
  2.   
  3. interface IDevInfoManager  
  4. {  
  5.     String getValue(String name);  
  6.     int update(String name, String value, int attribute);  
  7. }  
package android.app;

interface IDevInfoManager
{
    String getValue(String name);
    int update(String name, String value, int attribute);
}

2、添加相应的Service类,在frameworks/base/services/java/com/android/server下增加DevInfoService.java文件

DevInfoService.java文件如下:

[java] view plain copy print ?
  1. package com.android.server;  
  2.   
  3. import android.app.IDevInfoManager;  
  4. import android.util.Log;  
  5. import android.content.Context;  
  6. public class DevInfoService extends IDevInfoManager.Stub  
  7. {  
  8.     private static final String TAG = "DevInfoManagerService";  
  9.     private Context mContext;  
  10.     public DevInfoService(Context context){  
  11.         mContext = context;  
  12.     }  
  13.   
  14.     public String getValue(String name) {  
  15.         Log.d(TAG,"[getValue] name : "+name);  
  16.         return name;  
  17.     }  
  18.   
  19.     public int update(String name, String value, int attribute) {  
  20.         Log.d(TAG,"[update] name : "+name +", value : "+value+", attribute : "+attribute);  
  21.         return 0;  
  22.     }  
  23. }  
package com.android.server;

import android.app.IDevInfoManager;
import android.util.Log;
import android.content.Context;
public class DevInfoService extends IDevInfoManager.Stub
{
    private static final String TAG = "DevInfoManagerService";
    private Context mContext;
    public DevInfoService(Context context){
        mContext = context;
    }

    public String getValue(String name) {
        Log.d(TAG,"[getValue] name : "+name);
        return name;
    }

    public int update(String name, String value, int attribute) {
        Log.d(TAG,"[update] name : "+name +", value : "+value+", attribute : "+attribute);
        return 0;
    }
}

3、在SystemServer中生成该Service并添加到ServiceManager

frameworks/base/services/java/com/android/server/SystemServer.java文件:

[java] view plain copy print ?
  1. // add DevInfoService   
  2. try {  
  3.     Slog.i(TAG, "DevInfoManager Service");  
  4.     devInfo =  new DevInfoService(context);  
  5.     ServiceManager.addService(Context.DEVINFO_SERVICE, devInfo);  
  6. catch (Throwable e) {  
  7.     reportWtf("starting devInfo Service", e);  
  8. }  
  9. // end  
// add DevInfoService
try {
    Slog.i(TAG, "DevInfoManager Service");
    devInfo =  new DevInfoService(context);
    ServiceManager.addService(Context.DEVINFO_SERVICE, devInfo);
} catch (Throwable e) {
    reportWtf("starting devInfo Service", e);
}
// end

4、在frameworks/base/core/java/android/app/ContextImpl.java中增加service注册,如下:

[java] view plain copy print ?
  1. registerService(DEVINFO_SERVICE, new ServiceFetcher() {  
  2.         public Object createService(ContextImpl ctx) {  
  3.                 IBinder b = ServiceManager.getService(DEVINFO_SERVICE);  
  4.                 IDevInfoManager service = IDevInfoManager.Stub.asInterface(b);  
  5.                 return new DevInfoManager(service, ctx.mMainThread.getHandler());  
  6.         }  
  7. });  
registerService(DEVINFO_SERVICE, new ServiceFetcher() {
        public Object createService(ContextImpl ctx) {
                IBinder b = ServiceManager.getService(DEVINFO_SERVICE);
                IDevInfoManager service = IDevInfoManager.Stub.asInterface(b);
                return new DevInfoManager(service, ctx.mMainThread.getHandler());
        }
});

5、在frameworks/base/core/java/android/content/Context.java中增加Service名称定义:

[java] view plain copy print ?
  1. /** 
  2.  * Use with {@link #getSystemService} to retrieve a {@link 
  3.  * android.app.DevInfoManager} for handling management of 
  4.  * device information access. 
  5.  * 
  6.  * @see #getSystemService 
  7.  * @see android.app.DevInfoManager 
  8.  */  
  9. public static final String DEVINFO_SERVICE = "devinfo";  
/**
 * Use with {@link #getSystemService} to retrieve a {@link
 * android.app.DevInfoManager} for handling management of
 * device information access.
 *
 * @see #getSystemService
 * @see android.app.DevInfoManager
 */
public static final String DEVINFO_SERVICE = "devinfo";

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

@@ -112,6 +112,7 @@ LOCAL_SRC_FILES += \

        core/java/android/app/backup/IFullBackupRestoreObserver.aidl \

        core/java/android/app/backup/IRestoreObserver.aidl \

        core/java/android/app/backup/IRestoreSession.aidl \

+        core/java/android/app/IDevInfoManager.aidl \

        core/java/android/bluetooth/IBluetooth.aidl \

        core/java/android/bluetooth/IBluetoothA2dp.aidl \


7、进行编译,因为framework有变更,需要先update-api,在源码根目录下执行“make update-api”进行更新,完成后“make -j16”进行整体编译



 

你可能感兴趣的:(Android,系统源码)