繁体   English   中英

无法从 Android Q 中的“android.location.ILocationManager”中找到方法 reportLocation()

[英]Can not find method reportLocation() from "android.location.ILocationManager" in Android Q

我正在尝试通过“android.location.ILocationManager$Stub”的反射来获取方法 reportLocation()。

代码:

 Class<?> mStub = Class.forName("android.location.ILocationManager$Stub");
 Method getInterface = mStub.getMethod("asInterface", IBinder.class);

 Class mServiceManager = Class.forName("android.os.ServiceManager");
 Method getService = mServiceManager.getMethod("getService", String.class);

 Object iBindermInterface = getService.invoke(null, "location");
 Object mInterface = getInterface.invoke(null, (IBinder) iBindermInterface);

 Method method = mInterface.getClass().getMethod("reportLocation", Location.class, Boolean.class);

错误:

java.lang.NoSuchMethodException: android.location.ILocationManager$Stub$Proxy.reportLocation [class android.location.Location, class java.lang.Boolean]

当我尝试从“android.location.ILocationManager$Stub”打印所有方法时,我没有看到这个方法:

 Class<?> mStub = Class.forName("android.location.ILocationManager$Stub");
  Method[] getInterfaceAll = mStub.getMethods();
        for (Method getInt : getInterfaceAll)
          Log.d(LOG_TAG, "getInt = "+getInt);

在 Android P (SDK 28) 中我没有问题。

在 Android Q (SDK 29) 接口android.location.ILocationManager没有reportLocation()方法,相反,根据我的假设,接口com.android.internal.location.ILocationProviderManageronReportLocation() 但是这个接口包含抽象方法onReportLocation()并且我无法得到它的实现。

哪个接口包含此方法或它已更改(重命名)为另一个方法?

这就是使用带有反射的内部 API 的生命周期。

这个问题很简单,在reportLocation(in Location location, boolean passive)方法是目前Android的前9在ILocationManager.aidl接口,检查这里

但是从 Android 10 开始,该方法已被删除,您可以在此处的内部部分中看到

您可能应该更改您的代码以使用一些公共 API,否则您将很难扫描当前的 API 并找到可与反射一起使用的替代品。 (而且您将来仍然会冒着将 API 列入黑名单的风险)

PS:只是为了完整的信息,可能功能已经移到这里(我已经遵循了一些提交,但这可能完全无法使用/不同,我真的不鼓励你尝试使用它)

我发现了如何在报告中放置位置。

使用 public boolean injectionLocation(@NonNull Location newLocation) 方法,它可以从 ILocationManager 获得。

        Class<?> mStub = Class.forName("android.location.ILocationManager$Stub");
        Method getInterface = mStub.getMethod("asInterface", IBinder.class);
        Method getService = Class.forName("android.os.ServiceManager")
            .getMethod("getService", String.class);
        Object mInterface = getInterface
            .invoke(null, (IBinder) getService.invoke(null, "location"));
        Method method = mInterface.getClass()
            .getMethod("injectLocation", Location.class);
        method.invoke(mInterface, location);
       

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM