Fre*_*fri 8 reflection android android-ndk android-4.2-jelly-bean
我试图在Android 4.2.2的Nexus 4中设置飞机模式.我知道这是不可能的,因为AIRPLANE_MODE_ON已移至全局系统设置,它只是一个读取选项.
有没有其他方法可以做类似的事情,我的意思是禁用收音机?我可以禁用蓝牙,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);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以看到我替换了方法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)
Run Code Online (Sandbox Code Playgroud)
如果我使用<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>我得到错误Permission is only granted to system apps.我正在检查SDK源(在跟踪文件中),试图在没有此权限的情况下找到设置飞机模式的方法,但我没有取得任何成功.
从4.2.2开始,您无法切换飞行模式,因为它是只读的.
你有两个选择.
System程序.即根电话,将你的APK推送到/system/app那里安装.这将使您能够切换飞行模式.Reflection获得Android系统的功能调用,切换飞行模式的保持.您应该获得有关如何使用Reflection获取曝光API的代码示例,否则不会通过SDK向开发人员公开.