我的$ C $下连续切换飞行模式 [英] My code toggles airplane mode continuously

查看:236
本文介绍了我的$ C $下连续切换飞行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个做以下的屏幕(活动):它有一个切换按钮用来切换飞行模式;它通过使用催生了一个新的线程服务做的。它也有一个按钮,不以完全相同的方式完全一样的东西。实在是没有什么特别的,如下所示的code片段。虽然如预期一切正常的切换按钮(飞行模式改变为on如果当前的手机是不是在飞行模式,改变为关,如果目前在飞行模式),当单击该按钮时,飞机模式不断切换(飞行模式切换,从开到关,回到ON,然后关闭......),如果它落入循环。在互联网上经过一番研究,我怀疑这有事可做的意图/广播接收器为手机/服务状态在Android中被解雇的方式;作为切换按钮有两个状态,有效地prevented被再次广播的意图。它是否正确??如果是这样,这将是使用按钮(对一个单选按钮或切换按钮)切换飞行模式的正确方法吗?

  / **处理程序的按钮。 * /
runToggleAirplaneModeServiceBtn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
    startService(新意图(SleepScheduleController.this,AirplaneModeService.class));
}
});
/ **处理程序的切换按钮。 * /
airplaneModeToggleButton.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
    startService(新意图(SleepScheduleController.this,AirplaneModeService.class));
}
});
/ **在同一画面切换按钮和按钮。
 *需要这一次更新切换按钮的状态
 *在切换命令被执行。
 * /
IntentFilter的=新的IntentFilter(android.intent.action.SERVICE_STATE);
接收器=新的广播接收器(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        displayAirplaneMode();
    }};
registerReceiver(接收器,IntentFilter的);私人无效displayAirplaneMode(){
    如果(airplaneModeToggler.isInAirplaneMode()){
        airplaneModeToggleButton.setChecked(真);
        airplaneModeTextView.setText(R.string.airplane_mode_on);
    }其他{
     airplaneModeToggleButton.setChecked(假);
     airplaneModeTextView.setText(R.string.airplane_mode_off);
    }
}/ **在AirplaneModeService code段* /
@覆盖
公共无效的onCreate(){
    airplaneModeToggler =新AirplaneModeToggler(本);
    螺纹mThread =新主题(NULL,airplaneModeToggleTask,AirplaneModeToggleTask);
    mThread.start();
}私人Runnable接口airplaneModeToggleTask =新的Runnable(){
    @覆盖
    公共无效的run(){
        airplaneModeToggler.toggle(NULL);
        AirplaneModeService.this.stopSelf();
    }
};/ ** code段由AirplaneModeService使用的实用工具类。* /
公共布尔toggleAirplaneMode(布尔启用){
    布尔_enabling =使== NULL? !isInAirplaneMode():enabling.booleanValue();
Settings.System.putInt(mContext.getContentResolver(),
            Settings.System.AIRPLANE_MODE_ON,
            _enabling? AIRPLANE_MODE_ON:AIRPLANE_MODE_OFF);
    意向意图=新意图(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra(国家,_enabling);
    mContext.sendBroadcast(意向);
    返回_enabling;
}


解决方案

所以我追查错误。它是由切换按钮的OnCheckedChangeListener引起了我的code重新excuting命令。说如果切换命令是previously称为编程,或点击另一个按钮,那么这些将导致在切换按钮的选中状态的变化,并随后将执行同样的来回切换的命令,这将导致新一轮的检查状态变化的切换按钮。

I have a screen (Activity) that does the following: it has a toggle button that toggles the airplane mode; it does it by using a service that spawn off a new Thread. It also has a button that does exactly the same thing in exactly the same way. There is really nothing fancy as the code snippets below show. While everything works as expected for the toggle button (airplane mode changes to "on" if currently the phone is not in airplane mode; changes to "off" if currently in airplane mode), when the button is clicked, the airplane mode toggles continuously (airplane mode toggles from "on" to "off" and back to "on" and then to "off"...) as if it falls into a loop. After some research on the internet, I suspected this had something to do with the way the intent/broadcastReceiver for the phone/service state was fired in Android; as the toggle button had two states which effectively prevented the intent from being broadcast again. Is this correct?? If so, what would be the right way of toggling airplane mode using a button (vs. a radiobutton or a togglebutton)?

/** 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;
}

解决方案

So I tracked down the bug. It's caused by the OnCheckedChangeListener of the toggleButton re-excuting the command in my code. Say if the toggling command was previously called programmatically, or by clicking on the other button, then these would cause the checked state change on the toggleButton, which would subsequently execute the same toggling command, which would result in another round of checked state change on the toggleButton.

这篇关于我的$ C $下连续切换飞行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆