【问题标题】:My code toggles airplane mode continuously我的代码连续切换飞行模式
【发布时间】:2010-11-26 17:45:00
【问题描述】:

我有一个屏幕(活动),它执行以下操作:它有一个切换按钮,用于切换飞行模式;它通过使用产生新线程的服务来实现。它还有一个按钮,以完全相同的方式执行完全相同的操作。正如下面的代码 sn-ps 所示,真的没有什么花哨的。虽然切换按钮的一切都按预期工作(如果当前手机未处于飞行模式,飞行模式将变为“开启”;如果当前处于飞行模式,则变为“关闭”),当单击按钮时,飞行模式会连续切换(飞行模式从“开”切换到“关”,然后回到“开”,然后再切换到“关”......)就好像它陷入了一个循环一样。在互联网上进行了一些研究后,我怀疑这与在 Android 中触发电话/服务状态的意图/广播接收器的方式有关;因为切换按钮有两种状态,有效地阻止了意图再次被广播。它是否正确??如果是这样,使用按钮(与单选按钮或切换按钮相比)切换飞行模式的正确方法是什么?

/** Handler for the button. */
runToggleAirplaneModeServiceBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    startService(new Intent(SleepScheduleController.this, AirplaneModeService.class));
}
});
/** Handler for the toggle button. */
airplaneModeToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    startService(new Intent(SleepScheduleController.this, AirplaneModeService.class));
}
});
/** In the same screen as the toggle button and the button.
 * Need this to update the state of the toggle button once 
 * the toggling command is executed.
 */
intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");
receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        displayAirplaneMode();
    }};
registerReceiver(receiver, intentFilter);

private void displayAirplaneMode() {
    if(airplaneModeToggler.isInAirplaneMode()){
        airplaneModeToggleButton.setChecked(true);
        airplaneModeTextView.setText(R.string.airplane_mode_on);
    }else{
     airplaneModeToggleButton.setChecked(false);
     airplaneModeTextView.setText(R.string.airplane_mode_off);
    }
}

/** Code snippet in AirplaneModeService*/
@Override
public void onCreate() {
    airplaneModeToggler = new AirplaneModeToggler(this);
    Thread mThread = new Thread(null, airplaneModeToggleTask, "AirplaneModeToggleTask");
    mThread.start();
}

private Runnable airplaneModeToggleTask = new Runnable() {
    @Override
    public void run() {
        airplaneModeToggler.toggle(null);
        AirplaneModeService.this.stopSelf();
    }
};

/** Code snippet in the Utility class used by AirplaneModeService.*/
public Boolean toggleAirplaneMode(Boolean enabling) {
    boolean _enabling = enabling == null ? !isInAirplaneMode() : enabling.booleanValue();
Settings.System.putInt(mContext.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON, 
            _enabling ? AIRPLANE_MODE_ON : AIRPLANE_MODE_OFF);
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", _enabling);
    mContext.sendBroadcast(intent);
    return _enabling;
}

【问题讨论】:

  • 我回到我的代码并以编程方式切换了飞行模式,它确实开始了同样疯狂的循环。如果我使用后台服务或直接在活动中使用我的实用程序类,这似乎没有任何区别。当然,如果我没有注册意图,这个循环就不会发生;但随后我的 UI 不会更新以反映飞行模式已更改的事实:intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

标签: android button radio-button togglebutton airplane


【解决方案1】:

所以我找到了这个错误。这是由 toggleButton 的 OnCheckedChangeListener 重新执行我的代码中的命令引起的。假设之前以编程方式调用了切换命令,或者通过单击另一个按钮,那么这些将导致切换按钮上的选中状态更改,随后将执行相同的切换命令,这将导致另一轮选中状态更改切换按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多