• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps AirPlane ToggleButton???

I am trying to use a ToggleButton to switch AirPlane mode on and off. I am not sure how to go about this.

My permissions are:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
My .XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ToggleButton
android:id="@+id/Toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off">
</ToggleButton>
</LinearLayout>
My Java has this for the toggle button:

AirToggle = (ToggleButton) findViewById(R.id.Toggle);
AirToggle.setOnClickListener(new OnClickListener(){
public void onClick(View v){

if (((ToggleButton)v).isChecked()){
boolean isEnabled = Settings.System.getInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;
if(isEnabled == false)
{
Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,1); Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", 1);
context.sendBroadcast(intent);
}

IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
};

context.registerReceiver(receiver, intentFilter);


}

};
});
}

Please help.:confused:

-Thanks.:cool:
 
When I try to use Settings.System I get an Eclipse error saying it can't be resolved. This is the code:

boolean isEnabled = Settings.System.getInt(getApplicationContext().getContentResolver( ),Settings.System.AIRPLANE_MODE_ON, 0) == 1;

Can anyone see where I'm going wrong please?
 
When I try to use Settings.System I get an Eclipse error saying it can't be resolved. This is the code:

boolean isEnabled = Settings.System.getInt(getApplicationContext().getContentResolver( ),Settings.System.AIRPLANE_MODE_ON, 0) == 1;

Can anyone see where I'm going wrong please?

Did you try to add the follwing:

context=getAppicationContext();?


you should try to put:

context=getAppicationContext();
boolean isEnabled = Settings.System.getInt(getApplicationContext().getContentResolver( ),Settings.System.AIRPLANE_MODE_ON, 0) == 1;
 
Back
Top Bottom