基于android 12
添加服务
frameworks/base/services/java/com/android/server/SystemServer.java
在这两个函数中都可以添加:
startCoreServices()
startOtherServices()
比如说:
t.traceBegin("StartPowerStatsService");
// Tracks rail data to be used for power statistics.
mSystemServiceManager.startService(PowerStatsService.class);
t.traceEnd();
获取服务
frameworks/base/core/java/android/os/ServiceManager.java
比如说:
PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package");
例子:
下面通过VibratorManagerService看一个完整的例子:
- 首先aidl定义接口:
frameworks/base/core/java/android/os/IVibratorManagerService.aidl
/** {@hide} */
interface IVibratorManagerService {
int[] getVibratorIds();
VibratorInfo getVibratorInfo(int vibratorId);
boolean isVibrating(int vibratorId);
boolean registerVibratorStateListener(int vibratorId, in IVibratorStateListener listener);
boolean unregisterVibratorStateListener(int vibratorId, in IVibratorStateListener listener);
boolean setAlwaysOnEffect(int uid, String opPkg, int alwaysOnId,
in CombinedVibration vibration, in VibrationAttributes attributes);
void vibrate(int uid, String opPkg, in CombinedVibration vibration,
in VibrationAttributes attributes, String reason, IBinder token);
void cancelVibrate(int usageFilter, IBinder token);
}
- VibratorManagerService 继承 IVibratorManagerService.Stub 实现aidl接口
frameworks/base/services/core/java/com/android/server/vibrator/VibratorManagerService.java
public class VibratorManagerService extends IVibratorManagerService.Stub {
...
@Override // Binder call
public int[] getVibratorIds() {
return Arrays.copyOf(mVibratorIds, mVibratorIds.length);
}
@Override // Binder call
@Nullable
public VibratorInfo getVibratorInfo(int vibratorId) {
VibratorController controller = mVibrators.get(vibratorId);
return controller == null ? null : controller.getVibratorInfo();
}
@Override // Binder call
public boolean isVibrating(int vibratorId) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.ACCESS_VIBRATOR_STATE,
"isVibrating");
VibratorController controller = mVibrators.get(vibratorId);
return controller != null && controller.isVibrating();
}
...
}
- 客户端代理,App通过SystemVibratorManager调用, mService就是VibratorManagerService在客户端的实例
frameworks/base/core/java/android/os/SystemVibratorManager.java
public class SystemVibratorManager extends VibratorManager {
public SystemVibratorManager(Context context) {
super(context);
mContext = context;
mService = IVibratorManagerService.Stub.asInterface( // 通过aidl获取远程binder服务
ServiceManager.getService(Context.VIBRATOR_MANAGER_SERVICE));
}
@NonNull
@Override
public int[] getVibratorIds() {
synchronized (mLock) {
if (mVibratorIds != null) {
return mVibratorIds;
}
try {
if (mService == null) {
Log.w(TAG, "Failed to retrieve vibrator ids; no vibrator manager service.");
} else {
return mVibratorIds = mService.getVibratorIds();
}
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
return new int[0];
}
}
...
}
getSystemService流程:
context.getSystemService() -> contextImpl.getSystemService -> SystemServiceRegistry.getSystemService
从SYSTEM_SERVICE_FETCHERS获取的,put在registerService函数中.
registerService又封装了registerContextAwareService, registerStaticService,用于注册服务
frameworks/base/core/java/android/app/SystemServiceRegistry.java
public static Object getSystemService(ContextImpl ctx, String name) {
...
final ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
final Object ret = fetcher.getService(ctx);
...
}
private static <T> void registerService(@NonNull String serviceName,
@NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
SYSTEM_SERVICE_CLASS_NAMES.put(serviceName, serviceClass.getSimpleName());
}
// 在静态代码块中通过registerService注册服务
static {
// 注册VIBRATOR_MANAGER_SERVICE,实际new的SystemVibratorManager,
// VibratorManager是个抽象类, 实现的是 SystemVibratorManager
registerService(Context.VIBRATOR_MANAGER_SERVICE, VibratorManager.class,
new CachedServiceFetcher<VibratorManager>() {
@Override
public VibratorManager createService(ContextImpl ctx) {
return new SystemVibratorManager(ctx); //
}});
...
}
回到VibratorManagerService上,再看看VibratorManagerService如何通过jni到hal实现控制硬件zhendong
标签:java,vibratorId,SERVICE,int,添加,服务,Android,public,android
From: https://www.cnblogs.com/tangshunhui/p/16660564.html