Airplane mode in Jelly Bean
Asked Answered
H

1

8

I am trying to set the airplane mode in a Nexus 4 with Android 4.2.2. I know it is not possible since AIRPLANE_MODE_ON was moved to Global system settings and it is just a read option.

Is there any other way to do something similar, I mean disable the radio? I can disable Bluetooth, wifi and Internet connection, but the phone call network is still active.

Could be possible to create a library with the NDK to disable completely the network?

EDIT: I have tried with java.lang.reflect making this method:

@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);
    }
}

As you can see I replace the method Settings.System.putString(resolver, name, value); over JELLY_BEAN_MR1 but, of course, I am getting a SecurityException because my app is not a system app. This is the trace:

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)

If I use <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> I get the error Permission is only granted to system apps. I was checking the SDK sources (in the files from the trace) to try to find a way to set the airplane mode without this permission but I did not get any success.

Haemoglobin answered 22/4, 2013 at 23:11 Comment(2)
You'll find the answer to this question is already on this site! Try searching for "4.2 airplane mode". Basically, its no longer possible to change this setting from 4.2, unless your application has root (there is answers explaining how to do this too).Bithia
Thank for your answer. I am not actually looking for how to change this setting (airplane mode), anyway I search harder in Stackoverflow. I am looking for how to do something similar in a different way (if is possible). For my app use root permissions is not an option.Haemoglobin
A
5

As of 4.2.2 you cannot toggle Airplane mode as it has been Read-only.

You have two options.

  1. Make your application a System application. i.e. Root a phone, push your APK to /system/app and install from there. This will enable you to toggle airplane mode.
  2. Use Reflection to get hold of the Android system function call that toggles Airplane mode.

You should get code samples on how to use Reflection to get exposure API's otherwise not exposed a the developer through the SDK.

Amelita answered 23/4, 2013 at 1:29 Comment(5)
Could you give me any reference about how to use Reflection to get hold of the Android system function call?Haemoglobin
#38128 explains it a bit. I am sure you will find more resources as it is a commonly used method.Amelita
Thank you, but I did not be able to make it work. I have edited the the question.Haemoglobin
Looks like you are not doing it right. You will need to search a bit more on how Reflection is applied.Amelita
What system call? I haven't been able to find what I need to call through reflection to get it working.Tracheid

© 2022 - 2024 — McMap. All rights reserved.