1、在/frameworks/base/core/java/android/os下创建一个AIDL文件,比如IDeviceMgr.aidl
package android.os;
interface IDeviceMgr {int createApn(String apn);boolean deleteApn(int apnId);
}
编译系统,生成AIDL对应的JAVA文件。
2、在//frameworks/base/services/core/java/com/android/server创建包名devicemgr,并创建DeviceMgr,继承IDeviceMgr
package com.android.server.devicemgr;
import android.content.Context;
import android.os.IDeviceMgr;
import android.os.RemoteException;
public class DeviceMgr extends IDeviceMgr.Stub {private Context mContext;public DeviceMgr (Context context){this.mContext=context;}@Overridepublic int createApn(String apnInfo) throws RemoteException {return 0;}@Overridepublic boolean deleteApn(int apnId) throws RemoteException {return false;}
}
编译,然后,然后就出错了:
Your API changes are triggering API Lint warnings or errors.
To make these errors go away, fix the code according to the
error and/or warning messages above.If it is not possible to do so, there are workarounds:1. You can suppress the errors with @SuppressLint("<id>")where the <id> is given in brackets in the error message above.
2. You can update the baseline by executing the followingcommand:(cd $ANDROID_BUILD_TOP && cp \"out/soong/.intermediates/frameworks/base/api-stubs-docs-non-updatable/android_common/metalava/api_lint_baseline.txt" \"frameworks/base/services/api/lint-baseline.txt")To submit the revised baseline.txt to the main Androidrepository, you will need approval.
************************************************************
服务需要APP可以调用,选择方式2,执行:
cp out/soong/.intermediates/frameworks/base/api-stubs-docs-non-updatable/android_common/metalava/api_lint_baseline.txt frameworks/base/services/api/lint-baseline.txt
编译后继续出错:
******************************
You have tried to change the API from what has been previously approved.To make these errors go away, you have two choices:1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)to the new methods, etc. shown in the above diff.2. You can update current.txt and/or removed.txt by executing the following command:m api-stubs-docs-non-updatable-update-current-apiTo submit the revised current.txt to the main Android repository,you will need approval.
******************************
选择方法2,lunch相应product,执行:
m api-stubs-docs-non-updatable-update-current-api
这次不报错了
3、打开/frameworks/base/core/java/android/content/Context.java,添加一行对应的服务名称:
public static final String DEVICEMGR_SERVICE = "devicemgr";
这是APP调用getSystemService对应的服务名称
4、打开/frameworks/base/services/java/com/android/server/SystemServer.java,在中间加上一行代码:
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {.....................................ServiceManager.addService(Context.DEVICEMGR_SERVICE, new DeviceMgr(mSystemContext));.....................................
}