1.定义AIDL
frameworks\base\core\java\android\os\ICustomManager.aidl
java
package android.os;
/**
* {@hide}
*/
interface ICustomManager
{
String getValue();
}
2.定义服务
frameworks\base\services\core\java\com\android\server\hover\CustomManagerService.java
java
package com.android.server.hover;
import com.android.server.SystemService;
import android.content.Context;
import android.util.Log;
import java.util.HashMap;
import android.os.ICustomManager;
public final class CustomManagerService extends ICustomManager.Stub {
private static final String TAG = "CustomManagerService";
final Context mContext;
public CustomManagerService(Context context) {
mContext = context;
}
@Override
public String getValue() {
try {
return "Hello App";
} catch (Exception e) {
e.printStackTrace();
return "call error";
}
}
}
3.定义Manager
frameworks\base\core\java\android\os\CustomManager.java
java
package android.os;
import android.annotation.SystemService;
import android.content.Context;
import android.util.Log;
import android.os.Handler;
import android.os.SystemProperties;
import java.io.IOException;
import java.io.DataInputStream;
public final class CustomManager {
private static final String TAG = "CustomManager";
final Context mContext;
final ICustomManager mService;
/**
* {@hide}
*/
public CustomManager(Context context, ICustomManager service) {
mContext = context;
mService = service;
}
public String getValue() {
try {
return mService.getValue();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
4.注册服务
frameworks\base\services\java\com\android\server\SystemServer.java
java
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
....
CustomManagerService customService = null;
t.traceBegin("StartCustomManService");
try {
if(customService==null){
customService = new CustomManagerService(context);
}
ServiceManager.addService("custom", customService);
}catch (Throwable e) {
Slog.e(TAG, "Failure starting CustomManagerService ", e);
}
t.traceEnd();
....
}
frameworks\base\core\java\android\app\SystemServiceRegistry.java
java
static {
...
registerService("custom", CustomManager.class,
new CachedServiceFetcher<CustomManager>() {
@Override
public CustomManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow("custom");
ICustomManager service = ICustomManager.Stub.asInterface(b);
return new CustomManager(ctx.getOuterContext(), service);
}
});
...
}
5.忽略lint检查
bp
metalava_framework_docs_args = "" +
...
"--api-lint-ignore-prefix com.android. " +
"--api-lint-ignore-prefix android. " +
...
6.Selinux权限
system\sepolicy\prebuilts\api\34.0\public\service.te
erlang
...
type custom_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
...
system\sepolicy\prebuilts\api\34.0\private\service_contexts
erlang
...
window u:object_r:window_service:s0
custom u:object_r:custom_service:s0
* u:object_r:default_android_service:s0
...
7.系统应用调用服务接口
java
package com.ttit.helloapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.CustomManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomManager mCustomManager = getSystemService(CustomManager.class);
mCustomManager.getValue();
}
}