[英]Enable and Disable Airplane Mode successively Android
I am just a starter in Android. 我只是Android的首发。 I have an Android code which has a Button. 我有一个Android代码,它有一个Button。 On click of the button, it should Invoke AirPlane mode and then again back to normal mode. 单击该按钮,它应调用AirPlane模式,然后再次返回正常模式。 Here is my code : 这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load controls
tvStatus = (TextView)findViewById(R.id.tvStatus);
togState = (Button)findViewById(R.id.togState);
// set click event for button
togState.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
}
});
}
public void toggleAirplaneMode(boolean state) {
// toggle airplane mode
Settings.System.putInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1);
// broadcast an intent to inform
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !state);
sendBroadcast(intent);
}
public boolean isAirplaneMode() {
return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
}
} }
The problem here is, my phone will go in AirPlane mode and it toggles back also. 这里的问题是,我的手机将进入AirPlane模式,它也会切换回来。 But this process I cannot stop. 但这个过程我无法阻止。 Is the problem with the way I handled the OnClick Listener by calling same method (toggleAirplaneMode) twice? 我通过两次调用相同的方法(toggleAirplaneMode)处理OnClick侦听器的方式有问题吗?
Regards, 问候,
This answer contains code necessary to do this. 这个答案包含执行此操作所需的代码。 Also make sure you have the WRITE_SETTINGS permission. 还要确保您拥有WRITE_SETTINGS权限。
Adapted from Controlling Airplane Mode: 改编自控制飞行模式:
// read the airplane mode setting
boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
// toggle airplane mode
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
Replace the onClick
method with this: 用这个替换onClick
方法:
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
toggleAirplaneMode(!state);
super.handleMessage(msg);
}
};
Thread th = new Thread() {
@Override
public void run() {
toggleAirplaneMode(!state);
handler.sendEmptyMessage(0);
};
};
th.start();
}
Every time you will click the button, it will toggle the airplaneMode. 每次单击该按钮,它都会切换airplaneMode。 If it doesn't work, try removing !
如果它不起作用,请尝试删除!
I got it finally 我终于明白了
I used this in my code 我在我的代码中使用了这个
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
ser = new ServiceState();
ser.setState(STATE_IN_SERVICE);
}
And I have declared final int STATE_IN_SERVICE = 0; 我已经声明了final int STATE_IN_SERVICE = 0; before OnCreate. 在OnCreate之前。 And ser is the instance of ServiceState. ser是ServiceState的实例。
Thank you for your replies. 谢谢您的回复。
Check this out... This might help.. 看看这个...这可能有帮助..
public class MainActivity extends Activity { Context context; 公共类MainActivity扩展Activity {Context context;
private void changeRadioComponentEnabled(Context paramContext, String paramString, boolean paramBoolean1, boolean paramBoolean2)
{
boolean bool = false;
ContentResolver localContentResolver = paramContext.getContentResolver();
int i;
if (!paramBoolean1)
i = 1;
else
i = 0;
Settings.System.putInt(localContentResolver, "airplane_mode_on", i);
Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", paramString);
Intent localIntent = new Intent("android.intent.action.AIRPLANE_MODE");
if (!paramBoolean1)
bool = true;
localIntent.putExtra("state", bool);
paramContext.sendBroadcast(localIntent);
if (!paramBoolean2)
{
if (paramString.indexOf("cell") == 0)
Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell");
}
else
Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell,bluetooth,wifi,nfc");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", false, false);
}
});
((Button)findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", true, false);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.