Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn off airplane mode in Android

I would like to turn off the airplane mode if num>50, I implemented this code (from Toggle airplane mode in Android) but when executed I get a force close, can any one help here?

                if(num>50){
                    // read the airplane mode setting
                    boolean isEnabled = Settings.System.getInt(
                          getContentResolver(), 
                          Settings.System.AIRPLANE_MODE_ON, 0) == 1;

                    // toggle airplane mode
                    Settings.System.putInt(
                          getContentResolver(),
                          Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

                    // Post an intent to reload
                    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                    intent.putExtra("state", !isEnabled);
                    sendBroadcast(intent);



                }

o.k. I implemented the premonitions but i would like to change the if statement:

if num>=50 and airplane mode=on toggle it off 
if  airplane mode=off and num<50 toggle it on

Can some one help me writing the new code? (I'm a newbie)

like image 901
rami Avatar asked Dec 17 '22 10:12

rami


1 Answers

You most likely did not add WRITE_SETTING permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Also note that code:

 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
 intent.putExtra("state", !isEnabled);
 sendBroadcast(intent);

Is not supposed to work, because according to documentation on ACTION_AIRPLANE_MODE_CHANGED:

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

And even though you can currently send this broadcast without System permissions, it may change in future releases of Android.

like image 172
inazaruk Avatar answered Jan 08 '23 14:01

inazaruk