本文介绍了实现Android的飞行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做,我想显示一个弹出,如果飞行模式被激活,但是当我用下面的code,切换我的应用程序到飞行模式意味着我的设备进入飞行模式的应用程序。在那里,因为我想,如果飞行模式被激活,设备应该只给出一个弹出。我的code是如下:
公共无效飞机(){
最终布尔isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,1)== 0; //切换飞行模式
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,isEnabled? 0:1);
//后重新加载意图
//吐司TT = Toast.makeText(getApplicationContext(),您的手机处于飞行模式,Toast.LENGTH_SHORT);
// tt.show();
意向意图=新意图(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra(国家,isEnabled);
sendBroadcast(意向); IntentFilter的IntentFilter的=新的IntentFilter(android.intent.action.SERVICE_STATE); 广播接收器接收器=新的广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.d(飞行模式,服务状态变为);
}
}; context.registerReceiver(接收器,IntentFilter的); }
解决方案
在猜测我说你的问题就在于你的整数布尔code不正确。
尝试:
最终布尔isEnabled =(Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON,1))!= 0;
I am making an app in which I want to display a pop up if airplane mode is active but when I use the following code, it switches my app to the airplane mode means my device goes to airplane mode. Where as I want if airplane mode is active, a device should give a pop-up only. My code is as follows:
public void airplane() {
final boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 1) == 0;
// toggle airplane mode
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0: 1);
// Post an intent to reload
// Toast tt=Toast.makeText(getApplicationContext(), "Your phone is in airplane mode", Toast.LENGTH_SHORT);
// tt.show();
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", isEnabled);
sendBroadcast(intent);
IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
};
context.registerReceiver(receiver, intentFilter);
}
解决方案
At a guess I'd say your issue lies in the fact your integer to boolean code is incorrect.
Try:
final boolean isEnabled = (Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 1)) != 0;
这篇关于实现Android的飞行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!