Martin Pfeffer
Martin Pfeffer

Reputation: 12627

Switching to airplane mode using Android >4.4x

I need some help and hope to find an advice by asking you, clever people. I want to switch to airplane mode on Android JellyBean/Kitkat (API 17+)... I searched for hours but I don't get a working result/information. At the moment I could only switch WLAN and mobile data off - but cell connectivity will only be disabled after switching to airplane mode, right?

This works:

 public void manual_switch_off()
    {
        WifiManager wifiManager;
        wifiManager = (WifiManager) this.getSystemService(this.WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);
        ConnectivityManager dataManager;
        dataManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        Method dataMtd = null;
        try
        {
            dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
        } catch (NoSuchMethodException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dataMtd.setAccessible(true);
        try
        {
            dataMtd.invoke(dataManager, false);
        } catch (IllegalArgumentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

This second version (airplane mode) works not:

    @SuppressWarnings("deprecation")
public void airPlanemodeON()
{
    boolean isEnabled = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    if (isEnabled == false)
    {
        modifyAirplanemode(true);
        Toast.makeText(getApplicationContext(), "Airplane Mode ON", Toast.LENGTH_LONG).show();
    }
}

@SuppressWarnings("deprecation")
public void airPlanemodeOFF()
{
    boolean isEnabled = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    if (isEnabled == true)// means this is the request to turn ON AIRPLANE mode
    {
        modifyAirplanemode(false);
        Toast.makeText(getApplicationContext(), "Airplane Mode OFF", Toast.LENGTH_LONG).show();
    }
}

@SuppressWarnings("deprecation")
public void modifyAirplanemode(boolean mode)
{
    Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, mode ? 1 : 0);// Turning ON/OFF Airplane mode.

    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);// creating intent and Specifying action for AIRPLANE mode.
    intent.putExtra("state", !mode);// indicate the "state" of airplane mode is changed to ON/OFF
    sendBroadcast(intent);// Broadcasting and Intent

}

I set permissions to manifest. Some people said I could use Titanium Backup to switch this app to an system-app, will this really work? - I want to be able to load my application to the play store - I could imagine that after making my app an system app it wouldn't be possible any more, is this correct?

Thanks a lot!

Upvotes: 2

Views: 3040

Answers (1)

Ajay Pandya
Ajay Pandya

Reputation: 2457

Currently you can not achive turning airplane mode with higher than jellybean because google ristrict that as protected broadcast

Upvotes: 2

Related Questions