trouble using Airplane Mode on Android

1.4k views Asked by At

I have troube to use Airplane mode on my app. The app runs perfectly well on my emulator but not in my real phone when I try my apk.

Here is the code to activate the Airplane :

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        //intent.putExtra("state", !isEnabled);
        intent.putExtra("state", true);
        sendBroadcast(intent);

To desactivate it :

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        intent.putExtra("state", false);
        sendBroadcast(intent);

I read that I cannot create the intent (android doc) but I saw people that could make airplane mode so I don't understand!

Thanks to help me !

4

There are 4 answers

1
Vikram Bodicherla On

As a legal Android application, you cannot do any of what you are trying to do, including moving the phone into the airplane mode, disabling radios, turning off sound or anything of that sort.

Imagine how you, a user, would feel if some arbitrary application turned off your phone radio and you miss that call which informs you that you won a million dollars.

So what everyone is trying to get to, is that you as an application should not expect to move the phone into airplane mode. Only the user can do that.

Now if the user changes into the airplane mode, the system broadcasts an Intent, your application should catch that broadcast Intent and act on it. Acting on it means a variety of things, which include changing your UI.

Even if you find someway to achieve what you are trying to do today, the system can change tomorrow without warning (since it's an internal API/Intent) and your application will not function. It might, technically, not function on any other device except the one that you hold in your hand, you know the whole API contracts crap.

3
Noctuvigilus On

As you stated, the answer is in the documentation:

"This is a protected intent that can only be sent by the system."

Intent Documentation Reference

It may work on an emulator because the emulator isn't a secured device and lets you do things (like browse the file system) that you wouldn't normally be able to do. Also, you didn't mention what API level of Android you are trying to use. If it is something current, Google might have tightened down the restriction.

3
JoxTraex On

You CANNOT broadcast a System level intent. This is illegal by android's standards.

Instead of trying to send the intent, why don't you just implement a broadcast reciever to listen to that specific action and then when it changes, have your application handle it. This would be a much better approach. You cannot and should NOT expect to control airplane mode, but your app can listen to when it is invoked. You can also retrieve a property from the System database that tells you if its in airplane mode.

Once again Do not focus on trying to control it, just handle it.

0
Ashok Domadiya On
public static void setWifiOn(Context context) {
      WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    manager.setWifiEnabled(true);
    }

  public static boolean getAirplaneMode(Context context) {
      try {
      int airplaneModeSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
      return airplaneModeSetting==1?true:false;
    } catch (SettingNotFoundException e) {
      return false;
    }
  }