Android framwork service添加(manager 远程调service,service jni调native code)

本文详细介绍在Android系统中新增系统服务的具体步骤,包括JNI接口、AIDL接口定义、服务注册及远程调用等关键技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在平常android应用开发中,多数只是调framwork中的API进行application layer的coding,而在系统开发中可能会自己添加系统服务;
系统服务如任何添加,服务如何调native code,以及service如何被manager调用...这里我给出一个结果验证的demo。

1、实现编写native code,framwork/base/services/jni/com_android_server_VirtualInputService.cpp;

   以下是会使用到的native 函数,以及register_android_server_VirtualInputService;具体实现就不在详讲了。

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. static JNINativeMethod method_table[] = {  
  2.     {"native_open",             "()I",(void *)android_server_VirtualInputService_open},  
  3.     {"native_close",            "()Z",  (void *)android_server_VirtualInputService_close},  
  4.     {"native_sendir",       "(II)Z",  (void *)android_server_VirtualInputService_sendir},  
  5. };  
  6.      
  7. +int register_android_server_VirtualInputService(JNIEnv *env)  
  8. +{  
  9. +    jclass clazz = env->FindClass("com/android/server/VirtualInputService");  
  10. +    if (clazz == NULL) {  
  11. +        ALOGE("Can't find com/android/server/VirtualInputService");  
  12. +        return -1;  
  13. +    }  
  14. +     
  15. +    return jniRegisterNativeMethods(env, "com/android/server/VirtualInputService",  
  16. +            method_table, NELEM(method_table));  
  17. +}  
  18. +};  

2、base/services/jni/onload.cpp中添加register_android_server_VirtualInputService

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.  int register_android_server_SerialService(JNIEnv* env);  
  2. +int register_android_server_VirtualInputService(JNIEnv* env);  
  3.  int register_android_server_UsbDeviceManager(JNIEnv* env);  
  4.    
  5.       register_android_server_SerialService(env);  
  6. +   register_android_server_VirtualInputService(env);  
  7.      register_android_server_InputApplicationHandle(env);  

3、mk中添加com_android_server_VirtualInputService,使其被编译到

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. +++ base/services/jni/Android.mk    (working copy)  
  2. @@ -16,6 +16,7 @@  
  3.      com_android_server_VibratorService.cpp \  
  4.      com_android_server_location_GpsLocationProvider.cpp \  
  5.      com_android_server_connectivity_Vpn.cpp \  
  6. +    com_android_server_VirtualInputService.cpp \  
  7.      onload.cpp  

4、service添加: base/services/java/com/android/server/VirtualInputService.java   
   其必须继承IVirtualInputManager.Stub使其能被远程调用;
   声明native方法:private native static int native_open();
 private native static boolean native_close();
private native static boolean native_sendir(int keycode, int type);

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. +package com.android.server;  
  2. +  
  3. +import android.content.Context;  
  4. +import android.hardware.input.IVirtualInputManager;  
  5. +import android.util.Log;  
  6. +  
  7. +import java.io.File;  
  8. +import java.util.ArrayList;  
  9. +  
  10. +public class VirtualInputService extends IVirtualInputManager.Stub {  
  11. +    private static final String TAG = "VirtualInputManager";  
  12. +  
  13. +    private final Context mContext;  
  14. +    private int mVirtualInputFd = -1;  
  15. +  
  16. +    public VirtualInputService(Context context) {  
  17. +        mContext = context;  
  18. +       if(mVirtualInputFd == -1){  
  19. +           mVirtualInputFd = native_open();  
  20. +           Log.d(TAG, "mVirtualInputFd:"+mVirtualInputFd);  
  21. +       }else  
  22. +           Log.d(TAG, "mVirtualInputFd1:"+mVirtualInputFd);  
  23. +  
  24. +    }  
  25. +  
  26. +   public boolean SendIR(int keycode){  
  27. +       if(mVirtualInputFd > 0)  
  28. +       {     
  29. +           Log.d(TAG, "Service SendIR:"+keycode);  
  30. +           return native_sendir(keycode,0);  
  31. +       }  
  32. +       else   
  33. +           return false;  
  34. +   }  
  35. +     
  36. +    private native static int native_open();  
  37. +   private native static boolean native_close();  
  38. +   private native static boolean native_sendir(int keycode, int type);  
  39. +  
  40. +}    

5、base/services/java/com/android/server/SystemServer.java中添加新service


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. SerialService serial = null;  
  2. VirtualInputService VirtualInput = null;  
  3.  TwilightService twilight = null;  

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1.              try {  
  2. +                Slog.i(TAG, "VirtualInput Service");  
  3. +                VirtualInput = new VirtualInputService(context);  
  4. +                ServiceManager.addService(Context.VIRTUALINPUT_SERVICE, VirtualInput);  
  5. +            } catch (Throwable e) {  
  6. +                Slog.e(TAG, "Failure starting VirtualInputService", e);  
  7. +            }  
  8. +             
  9. +            try {  
  10.                  Slog.i(TAG, "Twilight Service");  
  11.                  twilight = new TwilightService(context);  
  12.              } catch (Throwable e) {  

6、添加aidl接口:base/core/java/android/hardware/input/IVirtualInputManager.aidl

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. +package android.hardware.input;  
  2. +  
  3. +  
  4. +/** @hide */  
  5. +interface IVirtualInputManager  
  6. +{  
  7. +   boolean SendIR(int keycode);  
  8. +}  

7、实现IVirtualInputManager.java;base/core/java/android/hardware/input/VirtualInputManager.java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. +package android.hardware.input;  
  2. +  
  3. +import android.app.PendingIntent;  
  4. +import android.content.Context;  
  5. +import android.os.Bundle;  
  6. +import android.os.ParcelFileDescriptor;  
  7. +import android.os.RemoteException;  
  8. +import android.os.SystemProperties;  
  9. +import android.util.Log;  
  10. +  
  11. +import java.io.IOException;  
  12. +import java.util.HashMap;  
  13. +  
  14. +/** 
  15. + * @hide 
  16. + */  
  17. +public class VirtualInputManager {  
  18. +    private static final String TAG = "VirtualInputManager";  
  19. +  
  20. +    private final Context mContext;  
  21. +    private final IVirtualInputManager mService;  
  22. +  
  23. +    /** 
  24. +     * {@hide} 
  25. +     */  
  26. +    public VirtualInputManager(Context context, IVirtualInputManager service) {  
  27. +        mContext = context;  
  28. +        mService = service;  
  29. +       Log.d(TAG,"VirtualInputManager");  
  30. +    }  
  31. +  
  32. +   public boolean SendIR(int keycode) throws android.os.RemoteException{  
  33. +       Log.d(TAG,"keycode:"+keycode);  
  34. +       return mService.SendIR(keycode);  
  35. +   }  
  36. +}  

8.framwork/base/Android.mk

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. core/java/android/hardware/input/IInputManager.aidl \  
  2. core/java/android/hardware/input/IVirtualInputManager.aidl \  
  3. core/java/android/hardware/input/IInputDevicesChangedListener.aidl \  

9、base/core/java/android/content/Context.java  中添加系统获取服务的标示 

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. +    public static final String VIRTUALINPUT_SERVICE = "virtualinput";  

10、base/core/java/android/app/ContextImpl.java

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. +        registerService(VIRTUALINPUT_SERVICE, new ServiceFetcher() {  
  2. +                public Object createService(ContextImpl ctx) {  
  3. +                    IBinder b = ServiceManager.getService(VIRTUALINPUT_SERVICE);  
  4. +                    return new VirtualInputManager(ctx, IVirtualInputManager.Stub.asInterface(b));  
  5. +                }});  
  6. +                 

   以上就是添加一个完整的service的步骤了...


原文地址:http://blog.youkuaiyun.com/cuityanxi/article/details/18082303

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值