0

有没有办法访问像 GnssMeasurements 这样的 android api 类的字段来设置值。在这个类中已经存在公共设置方法,但它们有一个@TestApi注释。我不知道如何访问这些方法或如何设置私有字段

我尝试通过以下方式访问私有字段:

    Class<?> c = GnssMeasurement.class;
    Object o = null;
    try {
        o = c.newInstance();

    } catch (IllegalAccessException | InstantiationException e) {
        e.printStackTrace();
    }

    assert cc != null;
    try {
        Field f1  = o.getClass().getDeclaredField("mPseudorangeRateMetersPerSecond");
        f1.setAccessible(true);
        f1.set(o, 123456.789);
        Double d = (Double) f1.get(o);
        System.out.println("field: " + d);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }

在这里,我得到了以下堆栈跟踪:

W/ocation_logger: Accessing hidden field Landroid/location/GnssMeasurement;->mPseudorangeRateMetersPerSecond:D (dark greylist, reflection)
W/System.err: java.lang.NoSuchFieldException: No field mPseudorangeRateMetersPerSecond in class Landroid/location/GnssMeasurement; (declaration of 'android.location.GnssMeasurement' appears in /system/framework/framework.jar)
W/System.err:     at java.lang.Class.getDeclaredField(Native Method)
        at com.example.simplelocation_logger2.MainActivity.initApp(MainActivity.java:287)
        at com.example.simplelocation_logger2.MainActivity.onCreate(MainActivity.java:152)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我也尝试访问这些方法:

    Class c;
    try {
        c = Class.forName("android.location.GnssMeasurement");
        Method m = c.getMethod("setPseudorangeRateMetersPerSecond", Double.class);
        Object o = m.invoke(null, 123456.789);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }

这给了我这个错误:

W/System.err: java.lang.NoSuchMethodException: setPseudorangeRateMetersPerSecond [class java.lang.Double]
W/System.err:     at java.lang.Class.getMethod(Class.java:2068)
        at java.lang.Class.getMethod(Class.java:1690)
        at com.example.simplelocation_logger2.MainActivity.initApp(MainActivity.java:299)
W/System.err:     at com.example.simplelocation_logger2.MainActivity.onCreate(MainActivity.java:152)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

有没有办法访问这些字段?

4

0 回答 0