Enable and Disable Airplane Mode successively Android -
i starter in android. have android code has button. on click of button, should invoke airplane mode , again normal mode. here 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 button togstate.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // check current state first boolean state = isairplanemode(); // toggle state toggleairplanemode(state); state = isairplanemode(); // toggle 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 intent 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, phone go in airplane mode , toggles also. process cannot stop. problem way handled onclick listener calling same method (toggleairplanemode) twice?
regards,
this answer contains code necessary this. make sure have write_settings permission.
adapted controlling airplane mode:
// read 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 intent reload intent intent = new intent(intent.action_airplane_mode_changed); intent.putextra("state", !isenabled); sendbroadcast(intent);
Comments
Post a Comment