简体   繁体   English

当我切换到飞行模式时,如何立即禁用完成按钮?

[英]How do I instantly disable my done button when I switch to airplane mode?

I have a done button in my app which I would like to disable when I switch to airplane mode. 我的应用程序中有一个完成按钮,当我切换到飞行模式时,我想禁用它。 I have working code for the same in my app. 我的应用程序中有相同的工作代码。 However, it doesn't disable as soon as I switch my app to airplane mode. 但是,只要我将应用程序切换到飞行模式,它就不会禁用。 It disables only when I switch to some other view and then return to this view. 它仅在我切换到其他视图然后返回此视图时禁用。 Is there a way I can instantly disable it, the moment my app detects offline mode? 我的应用程序检测到离线模式的那一刻,有没有办法可以立即禁用它? Here's my code so far: 到目前为止,这是我的代码:

  private static boolean isAirplaneModeOn(Context context) {

            return Settings.System.getInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;

        }

In my oncreateview, I have: 在我的oncreateview中,我有:

if(isAirplaneModeOn(Application.getAppContext())){
            Toast toast = Toast.makeText(this.getActivity(),getResources().getString(R.string.offline), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            mDoneBtn.setActivated(false);
            mDoneBtn.setTextColor(Color.DKGRAY);
            mDoneBtn.setOnClickListener(null);

        }else {
            mDoneBtn.setOnClickListener(this);
        }

Is there any other way of doing this which is better? 有没有其他方法可以做到这一点哪个更好? or can this be improved to achieve my goal? 或者可以改进以实现我的目标? Any ideas? 有任何想法吗? Thanks! 谢谢!

Why not trying disabling it: 为什么不尝试禁用它:

IntentFilter intentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");

BroadcastReceiver receiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
       if (!isAirplaneModeOn(this)) { 
          mDoneBtn.setEnabled(false);
       } else { 
          mDoneBtn.setEnabled(true); 
       } 
  }
 };

context.registerReceiver(receiver, intentFilter);

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterRecievers();
}

private void unregisterRecievers() {
    try {
        unregisterReceiver(receiver);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用AIRPLANE MODE平板电脑支持4.4.2 - How Do I Disable AIRPLANE MODE tablet supports 4.4.2 android-如果手机处于飞行模式或不在网络中,如何获取电话? - android - How do I get the phone if it is in airplane mode or not in the network? 如何启用/禁用飞行模式? - How to enable/disable airplane mode? 如何使用Switch禁用Android中的Seekbar? - How do I disable my Seekbar in Android with a Switch? 我如何记得按下完成按钮时所做的选择? - How do I remember my selection with the press of a done button? 我想在定时器完成后关闭按钮 - I want to switch button off when the timer is done 当我的应用与之无关时,为什么我将“ Settingplane_mode_on已从android.provider.Settings.System […]”移到了logcat中? - Why I have “Setting airplane_mode_on has moved from android.provider.Settings.System […]” into the logcat when my app has nothing to do with that? 如何在Android软键盘上隐藏/禁用完成按钮? - How can i hide/disable done button on soft keyboard android? 当我按下按钮时,我希望它告诉我我的位置(文本到语音),这是如何完成的? Android工作室 - When I press the button I want it to tell me my location (text to speech), how is this done? Android studio 即使应用程序关闭,如何暂时禁用按钮? - How do I disable a button temporarily even when the app is close?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM