0

我只是Android的初学者。我有一个带有按钮的 Android 代码。单击按钮后,它应该调用 AirPlane 模式,然后再次返回正常模式。这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // load controls
    tvStatus = (TextView)findViewById(R.id.tvStatus);
    togState = (Button)findViewById(R.id.togState);

    // set click event for button
    togState.setOnClickListener(new OnClickListener() {                     
                    @Override
                    public void onClick(View v) {
                            // check current state first
                            boolean state = isAirplaneMode();
                            // toggle the state
                            toggleAirplaneMode(state);

                            state = isAirplaneMode();
                            // toggle the state
                            toggleAirplaneMode(state);

                    }
            });
}

public void toggleAirplaneMode(boolean state) {
    // toggle airplane mode
    Settings.System.putInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1);

    // broadcast an intent to inform
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", !state);
    sendBroadcast(intent);
}



public boolean isAirplaneMode() {
    return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
}

}

这里的问题是,我的手机将进入 AirPlane 模式并且它也会切换回来。但这个过程我无法停止。我通过两次调用相同的方法(toggleAirplaneMode)来处理 OnClick 侦听器的方式有问题吗?

问候,

4

4 回答 4

2

此答案包含执行此操作所需的代码。还要确保您具有 WRITE_SETTINGS 权限。

改编自控制飞行模式:

// 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);
于 2013-08-13T11:40:37.190 回答
0

用这个替换onClick方法:

                public void onClick(View v) {
                        // check current state first
                        boolean state = isAirplaneMode();
                        // toggle the state
                        final Handler handler = new Handler(){
                            @Override
                            public void handleMessage(Message msg) {
                                toggleAirplaneMode(!state);
                                super.handleMessage(msg);
                            }       
                        };
                        Thread th = new Thread() {
                           @Override
                           public void run() {
                              toggleAirplaneMode(!state);
                              handler.sendEmptyMessage(0);
                           };
                        };
                        th.start();
                }

每次单击该按钮时,它都会切换飞机模式。如果不起作用,请尝试删除!

于 2013-08-13T11:38:36.317 回答
0

我终于明白了

我在我的代码中使用了这个

            public void onClick(View v) {
                // check current state first
                boolean state = isAirplaneMode();
                // toggle the state
                toggleAirplaneMode(state);

               state = isAirplaneMode();
                // toggle the state
                toggleAirplaneMode(state);
                ser = new ServiceState();
                ser.setState(STATE_IN_SERVICE);
               }

我已经宣布 final int STATE_IN_SERVICE = 0; 在 OnCreate 之前。ser 是 ServiceState 的实例。

谢谢您的回复。

于 2013-08-14T05:22:20.190 回答
0

看看这个...这可能会有所帮助..

公共类 MainActivity 扩展 Activity { 上下文上下文;

private void changeRadioComponentEnabled(Context paramContext, String paramString, boolean paramBoolean1, boolean paramBoolean2)
  {
    boolean bool = false;
    ContentResolver localContentResolver = paramContext.getContentResolver();
    int i;
    if (!paramBoolean1)
      i = 1;
    else
      i = 0;
    Settings.System.putInt(localContentResolver, "airplane_mode_on", i);
    Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", paramString);
    Intent localIntent = new Intent("android.intent.action.AIRPLANE_MODE");
    if (!paramBoolean1)
      bool = true;
    localIntent.putExtra("state", bool);
    paramContext.sendBroadcast(localIntent);
    if (!paramBoolean2)
    {
      if (paramString.indexOf("cell") == 0)
        Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell");
    }
    else
      Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell,bluetooth,wifi,nfc");
  }




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

    this.context = this;
    ((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", false, false);
      }
    });

    ((Button)findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
          MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", true, false);
      }
    });



}
于 2014-07-01T04:56:32.053 回答