Android BluetoothService类的反射问题
我想调用BluetoothService类的isEnabled()、getAddressFromObjectPath()等方法,但该类带有@hide标记。
我知道我有两种可能的方法来做我想做的事,一种是删除@hide,另一种是使用反射。我选择使用第二个。
从源代码的示例中,我发现
Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder b = (IBinder) method.invoke(null, "bluetooth");
if (b == null) {
throw new RuntimeException("Bluetooth service not available");
}
IBluetooth mBluetoothService = IBluetooth.Stub.asInterface(b);
,尽管BluetoothService确实扩展了IBluetooth.Stub,但它得到的是IBluetooth而不是BluetoothService。
所以我的问题如下:
(1)我可以像前面的示例代码一样通过反射获取BluetoothService类吗? (2)如果我的第一个问题是否定的,我直接通过反射方法调用 getAddressFromObjectPath() ,就像遵循
Method method = Class.forName("android.server.BluetoothService").getMethod("getAddressFromObjectPath", String.class);
String b = (String) method.invoke(???, PATH);
我需要在 invoke() 方法中填写的对象剂量一样,BluetoothService ???
任何建议将不胜感激!
I want to call some methods like isEnabled(), getAddressFromObjectPath(), etc. of BluetoothService class, but this class is mark with @hide.
I know I have two possible ways to do what I want, one is remove the @hide, and the other is using reflection. I choose to use second one.
From the example of source code, I found that
Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder b = (IBinder) method.invoke(null, "bluetooth");
if (b == null) {
throw new RuntimeException("Bluetooth service not available");
}
IBluetooth mBluetoothService = IBluetooth.Stub.asInterface(b);
However, what it gets is the IBluetooth not BluetoothService although BluetoothService extends IBluetooth.Stub indeed.
So my questions are as follows:
(1) Could I get the BluetoothService class by reflection just like previous example code ?
(2) If my first question is negative, I call getAddressFromObjectPath() directly by reflection method like following
Method method = Class.forName("android.server.BluetoothService").getMethod("getAddressFromObjectPath", String.class);
String b = (String) method.invoke(???, PATH);
what the object dose I need to fill in the invoke() method, BluetoothService ???
Any suggestion would be greatly appreciated !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过网上查资料,我得到了答案。如果我想调用非静态方法,我需要首先获取类和构造函数。使用构造函数构造实例,然后我可以通过该实例调用非静态方法。
但是,我无法在 BluetoothService 类上执行此操作,因为如果我再次执行构造函数,则会导致很多问题!
我决定修改 IBluetooth.aidl 以添加我需要的方法,因为 BluetoothService 扩展了 IBluetooth。如果我可以获得 IBluetooth 实例,我就可以调用我需要的方法。也许,这不是一个好的解决方案,但我认为它会起作用。
多谢。
After surveying on the internet, I got the answer. If I want to invoke a non-static method, I need to get the class and constructor first. Use the constructor to construct the instance and then I could invoke the non-static method by this instance.
However, I could not do that on BluetoothService class because if I do the constructor again, it would cause a lot of problem !
I decide to modify the IBluetooth.aidl to add the methods I need because BluetoothService extends IBluetooth. If I could get the IBluetooth instance, I could call the methods I need. Maybe, this is not a good solution but I think it would work.
Thanks a lot.