上层应用调用自定义的系统服务注意事项

 

【自定义的系统服务注册】

1. frameworks/base/services/java/com/android/server/SystemServer.java

当中添加

ServiceManager.addService("服务注册名称",new LianjiaoService(context));

ServiceManager.addService("LianjiaoService",new LianjiaoService(context));

上层应用使用 服务注册名称 查找到服务,

我们这边是使用的是

ServiceManager.addService("LianjiaoService",new LianjiaoService(context));

之前的代码是使用

IBinder localIBinder1 = ServiceManager.getService("lianjiao");
this.mLianjiaoService = ILianjiaoService.Stub.asInterface(localIBinder1);

 

结果:

1. 之前的代码在新平台上面不能查找到服务,所以相应的功能就使用不了

解决方案:

1. 将服务注册名称修改为之前的服务注册名称 lianjiao 就可以了

 

【总结】

1. 需求方定义 服务注册名称

 

【AIDL当中添加API需要注册事项】

如一个AIDL文件定义如下:

interface ILianjiaoService{
boolean startCommunication();
boolean stopCommunication();
boolean setProfileModel(int mode);
int getProfileModel();
boolean closeBottomTaskBar();
boolean openBottomTaskBar();
boolean setStatusBarDisable(int state);
boolean shutdownSystem();
boolean installApplication(String packageName, boolean paramBoolean);
boolean uninstallApplication(String packageName);
boolean addUrl(String url);
boolean deleteUrl(String url);
boolean openBackups();
boolean closeBackups();
}

在转换JAVA代码如下:

      public static abstract class Stub
  extends Binder
  implements ILianjiaoService
{
  private static final String DESCRIPTOR = "android.app.ILianjiaoService";
  static final int TRANSACTION_startCommunication = 1;
  static final int TRANSACTION_stopCommunication = 2;
  static final int TRANSACTION_setProfileModel = 3;
  static final int TRANSACTION_getProfileModel = 4;
  static final int TRANSACTION_closeBottomTaskBar = 5;      
  static final int TRANSACTION_openBottomTaskBar = 6;
  static final int TRANSACTION_setStatusBarDisable = 7;
  static final int TRANSACTION_shutdownSystem = 8;     
  static final int TRANSACTION_installApplication = 9;                
  static final int TRANSACTION_uninstallApplication = 10;       
  static final int TRANSACTION_addUrl = 11;
  static final int TRANSACTION_deleteUrl = 12;
  static final int TRANSACTION_openBackups = 13;
  static final int TRANSACTION_closeBackups = 14;

 

结论:

1. AIDL 当中的每一个方法在JAVA当中均对应一个常量,如startCommunication方法 对应TRANSACTION_startCommunication = 1

2. 方法对应的常量编制规则:按AIDL当中定义的顺序 从1开始自增1

3. 当需要在AIDL当中添加新的API的时候,不会影响新程序在老系统当中调用老API,只是调用不了新API

4. 新老系统的AIDL变化会兼容只要API定义顺序

public static abstract class Stub
  extends Binder
  implements ILianjiaoService
{
  private static final String DESCRIPTOR = "android.app.ILianjiaoService";
  static final int TRANSACTION_startCommunication = 1;
  static final int TRANSACTION_stopCommunication = 2;
  static final int TRANSACTION_setProfileModel = 3;
  static final int TRANSACTION_getProfileModel = 4;
  static final int TRANSACTION_closeBottomTaskBar = 5;
  static final int TRANSACTION_openBottomTaskBar = 6;
  static final int TRANSACTION_setStatusBarDisable = 7;
  static final int TRANSACTION_shutdownSystem = 8;
  static final int TRANSACTION_installApplication = 9;
  static final int TRANSACTION_uninstallApplication = 10;
  static final int TRANSACTION_addUrl = 11;
  static final int TRANSACTION_deleteUrl = 12;


  static final int TRANSACTION_setAPKInstallFlag = 13;
  static final int TRANSACTION_openGprs = 14;
  static final int TRANSACTION_closeGprs = 15;
  static final int TRANSACTION_openApplication = 16;
  static final int TRANSACTION_closeApplication = 17;
  static final int TRANSACTION_openBackups = 18;
  static final int TRANSACTION_closeBackups = 19;
  static final int TRANSACTION_setPackageName = 20;

 

如:在老系统当中添加新系统的两个相同的方法,但在新系统当中已经添加了许多其他的方法,导致方法所对应的常量在老新系统当中不一样,

导致方法不兼容。

 

解决方案:

1. 需求方定义AIDL文件,不要让各个外包方定义AIDL文件

 

【总结】

1. 需求方定义AIDL文件,不要让各个外包方定义AIDL文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值