[英]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.