我正在使用aidl自动应答呼叫,代码如下:
ITelephony.Stub.asInterface(ServiceManager.getService("phone"))
.answerRingingCall();我导入ServiceManager.class
import android.os.ServiceManager;但是有一个问题:导入的android.os.ServiceManager无法解决
我怎么才能让它工作呢?谢谢
发布于 2012-02-15 19:28:23
android.os.ServiceManager是一个隐藏类(即@hide),并且隐藏类(即使它们在android.jar意义上是公共的)被从android.jar中删除,因此当您尝试导入ServiceManager时会出现错误。隐藏类是Google不希望成为文档公共API的一部分的类。
使用非公共API的应用程序不容易编译,这个类将有不同的平台版本。
发布于 2017-06-27 07:05:18
虽然这是一个旧的问题,但还没有人回答。任何隐藏的类都可以通过反射API使用。以下是通过反射API使用service Manager获取服务的示例:
if(mService == null) {
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, "My_SERVICE_NAME");
if(binder != null) {
mService = IMyService.Stub.asInterface(binder);
}
if(mService != null)
mIsAcquired = true;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Service is already acquired");
}发布于 2018-12-16 15:03:40
如上所述,这些方法只适用于Android N on words的系统应用程序或框架应用程序。我们仍然可以使用Android代码的反射来为ServiceManager使用系统应用程序编写代码,如下所示
@SuppressLint("PrivateApi")
public IMyAudioService getService(Context mContext) {
IMyAudioService mService = null;
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, "YOUR_METHOD_NAME");
if (binder != null) {
mService = IMyAudioService .Stub.asInterface(binder);
}
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return mService;
}https://stackoverflow.com/questions/4446469
复制相似问题