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);
}
};
});
}
UPDATE: This is my log cat errors if that helps.
04-19 18:43:24.264: ERROR/AndroidRuntime(233): ERROR: thread attach failed
04-19 18:43:25.933: ERROR/AndroidRuntime(241): ERROR: thread attach failed
04-19 18:43:31.543: ERROR/AndroidRuntime(248): Uncaught handler: thread main exiting due to uncaught exception
04-19 18:43:31.553: ERROR/AndroidRuntime(248): java.lang.NullPointerException
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at com.simple.armor.SimpleArmor$1.onClick(SimpleArmor.java:30)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.View.performClick(View.java:2364)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.widget.CompoundButton.performClick(CompoundButton.java:98)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.View.onTouchEvent(View.java:4179)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.widget.TextView.onTouchEvent(TextView.java:6541)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.View.dispatchTouchEvent(View.java:3709)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.os.Looper.loop(Looper.java:123)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at java.lang.reflect.Method.invoke(Method.java:521)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-19 18:43:31.553: ERROR/AndroidRuntime(248): at dalvik.system.NativeStart.main(Native Method)
04-19 18:43:31.573: ERROR/dalvikvm(248): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Please help.
-Thanks.
See comments attached to question. The variable 'context' was not assigned, resulting in a Null Reference Exception. There isn't anything wrong with the logic behind setting the Air Plane mode setting.