飞机切换按钮?

发布于 2024-11-02 10:37:57 字数 5290 浏览 0 评论 0原文

我正在尝试使用 ToggleButton 来打开和关闭飞行模式。我不知道该怎么做。

我的权限是:

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

我的 .XML 文件如下所示:

<?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>

我的 Java 具有切换按钮:

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);


            }

    };
});

}

更新: 这是我的日志猫错误,如果有帮助的话。

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

请帮忙。

-谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海的爱人是光 2024-11-09 10:37:57

请参阅问题所附评论。未分配变量“context”,导致空引用异常。设置飞行模式设置背后的逻辑没有任何问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文