果冻豆的飞机模式

时间:2013-04-22 23:12:00

标签: android reflection android-ndk android-4.2-jelly-bean

我正在尝试使用Android 4.2.2在Nexus 4中设置飞机模式。 我知道这是不可能的,因为AIRPLANE_MODE_ON已移至Global system settings并且它只是一个读取选项。

有没有其他方法可以做类似的事情,我的意思是禁用收音机?我可以禁用蓝牙,wifi和互联网连接,但电话呼叫网络仍处于活动状态。

是否可以使用NDK创建一个完全禁用网络的库?

编辑:我尝试使用java.lang.reflect制作此方法:

@SuppressLint("NewApi")
@SuppressWarnings("rawtypes")
private boolean putStringAirplaneMode() throws ClassNotFoundException,
        NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {

    ContentResolver resolver = context.getContentResolver();
    String name = Settings.Global.AIRPLANE_MODE_ON;
    String value = (isEnabled() ? "1" : "0");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Log.v("AirplaneBatterySaver",
                "Using reflection to change airplane mode");
        // For JELLY_BEAN_MR1 use reflection. TODO test if works reflection
        Class SystemProperties = android.provider.Settings.Global.class;

        Class[] parameterTypes = new Class[3];
        parameterTypes[0] = ContentResolver.class;
        parameterTypes[1] = String.class;
        parameterTypes[2] = String.class;

        @SuppressWarnings("unchecked")
        Method method = SystemProperties.getMethod("putString",
                parameterTypes);

        method.setAccessible(true);
        return (Boolean) method.invoke(new Object(), resolver, name, value);
        // return Settings.Global.putString(resolver, name, value);

    } else {
        Log.v("AirplaneBatterySaver",
                "Using Settings.System to change airplane mode");
        return Settings.System.putString(resolver, name, value);
    }
}

正如您所看到的,我将方法Settings.System.putString(resolver, name, value);替换为JELLY_BEAN_MR1,但当然,我得到的是SecurityException,因为我的应用不是系统应用。这是跟踪:

Caused by: java.lang.SecurityException: Permission denial: writing to secure 
    settings requires android.permission.WRITE_SECURE_SETTINGS
at android.os.Parcel.readException(Parcel.java:1425)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:574)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:777)
at android.provider.Settings$Global.putStringForUser(Settings.java:5421)
at android.provider.Settings$Global.putString(Settings.java:5411)

如果我使用<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>,我会收到错误Permission is only granted to system apps。我正在检查SDK源(在跟踪文件中),试图在没有此权限的情况下找到设置飞机模式的方法,但我没有取得任何成功。

1 个答案:

答案 0 :(得分:5)

自4.2.2起,您无法切换飞行模式,因为它是只读的。

您有两种选择。

  1. 将您的应用程序设为System个应用程序。即根电话,将你的APK推送到/system/app并从那里安装。这将使您能够切换飞行模式。
  2. 使用Reflection获取切换飞行模式的Android系统函数调用。
  3. 您应该获得有关如何使用Reflection获取曝光API的代码示例,否则不会通过SDK向开发人员公开。

相关问题