关闭Android飞行模式

4
如果 num>50,我想关闭飞行模式。 我实现了这个代码(来自在安卓上切换飞行模式),但是当我执行时,我遇到了强制关闭的问题,请问有人可以帮助吗?
                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);



                }

好的,我已经实现了预测,但我想要改变if语句:

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

有人能帮我写新代码吗?(我是新手)


5
保持冷静,接受回答并感谢那些对你有帮助的问题回答! - evilone
3个回答

8

您很可能没有在AndroidManifest.xml中添加WRITE_SETTING权限:

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

另外请注意代码:

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

根据ACTION_AIRPLANE_MODE_CHANGED的文档,它不应该起作用:

这是一个受保护的意图,只能由系统发送。

即使您目前可以在没有系统权限的情况下发送此广播,但在未来的Android版本中可能会发生更改。


3

1

请参考以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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");
                Toast.makeText(getApplicationContext(), "Service state changed", Toast.LENGTH_LONG).show();
                boolean isEnabled = isAirplaneModeOn(context);
            /*   setSettings(context, isEnabled?1:0);
                Intent intent_mode = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
               intent_mode.putExtra("state", !isEnabled);
                context.sendBroadcast(intent_mode);*/

                if(isEnabled==true)
                { setSettings(context, isEnabled?1:0);
                    Toast.makeText(getApplicationContext(), "Flight mode on", Toast.LENGTH_LONG).show();
                    Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
                    Intent newIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
                    newIntent.putExtra("state", false);
                    sendBroadcast(newIntent);
                }
                else
                { setSettings(context, isEnabled?1:0);
                    Toast.makeText(getApplicationContext(), "Flight mode off", Toast.LENGTH_LONG).show();
                }

          }

        @SuppressLint("NewApi")
        private void setSettings(Context context, int value) {
            // TODO Auto-generated method stub

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Settings.System.putInt(
                          context.getContentResolver(),
                          Settings.System.AIRPLANE_MODE_ON, value);
            } else {
                Settings.Global.putInt(
                          context.getContentResolver(),
                          Settings.Global.AIRPLANE_MODE_ON, value);
            }       

        }

        @SuppressLint("NewApi")
        public boolean isAirplaneModeOn(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.System.getInt(context.getContentResolver(), 
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
        } else {
            return Settings.Global.getInt(context.getContentResolver(), 
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        }       
    }
    };

    registerReceiver(receiver, intentFilter);


}

//permissions needed:

//

//

描述你做了哪些更改? - Avijit
首先,使用广播接收器使其在后台运行...然后查看用于版本相关问题的setSettings()方法...以及IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE"); 是重要的部分...如果您的问题得到了解答,请告诉我。 - Harshal Benake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接