无法解析导入android.os.ServiceManager
我正在使用aidl自动接听电话,代码如下:无法解析导入android.os.ServiceManager,android,aidl,Android,Aidl,我正在使用aidl自动接听电话,代码如下: ITelephony.Stub.asInterface(ServiceManager.getService("phone")) .answerRingingCall(); 我导入ServiceManager.class import android.os.ServiceManager; 但有一个问题:无法解决导入android.os.ServiceManager的问题 我怎样才能让它工作?谢谢android.os.ServiceManage
ITelephony.Stub.asInterface(ServiceManager.getService("phone"))
.answerRingingCall();
我导入ServiceManager.class
import android.os.ServiceManager;
但有一个问题:无法解决导入android.os.ServiceManager的问题
我怎样才能让它工作?谢谢
android.os.ServiceManager
是一个隐藏类(即,@hide
),隐藏类(即使它们在Java意义上是公共的)从android.jar中删除,因此当您尝试导入ServiceManager
时会出现错误。隐藏类是Google不想成为有文档记录的公共API的一部分的类
使用非公共API的应用程序无法轻松编译,该类将有不同的平台版本。虽然它是旧版本,但还没有人回答它。任何隐藏类都可以使用反射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");
}
如上所述,这些方法仅适用于Android N上的系统应用程序或框架应用程序。 尽管如此,我们仍然可以使用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;
}
因此,我无法在我们的应用程序中使用此ServiceManager类。@Ramesh_D是的,这是可能的,请参阅下面Vinayak的回复,它对我来说很好。什么是创建或导入IMyAudioService?什么是MSService?我已经解决了这两个问题而没有编译(什么是IMyService?什么是mService?我已经解决了这两个问题而没有编译)(