android - Turn Off Airplane/Flight Mode automatically if ON -



android - Turn Off Airplane/Flight Mode automatically if ON -

i have written code to on/off airplane/flight mode programmatically, , still using two different buttons command that, 1 on aeroplane mode , sec off aeroplane mode, using below code:

@suppresswarnings("deprecation") public void airplanemodeon(view v) { boolean isenabled = settings.system.getint(this.getcontentresolver(), settings.system.airplane_mode_on, 0) == 1; if (isenabled == false) { // means request turn off aeroplane mode modifyairplanemode(true); // on toast.maketext(getapplicationcontext(), "airplane mode on", toast.length_long).show(); } } @suppresswarnings("deprecation") public void airplanemodeoff(view v) { boolean isenabled = settings.system.getint(this.getcontentresolver(), settings.system.airplane_mode_on, 0) == 1; if (isenabled == true) // means request turn on aeroplane mode { modifyairplanemode(false); // off toast.maketext(getapplicationcontext(), "airplane mode off", toast.length_long).show(); } } @suppresswarnings("deprecation") public void modifyairplanemode(boolean mode) { settings.system.putint(getcontentresolver(), settings.system.airplane_mode_on, mode ? 1 : 0);// turning on/off aeroplane mode. intent intent = new intent(intent.action_airplane_mode_changed);// creating intent , specifying action aeroplane mode. intent.putextra("state", !mode);// indicate "state" of aeroplane mode changed on/off sendbroadcast(intent);// broadcasting , intent }

but want know status of airplane mode in every 2 seconds have written timer code, , if aeroplane mode on want turn off automatically:

@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); starttimer(); } @override protected void onresume() { super.onresume(); //onresume start our timer can start when app comes background starttimer(); } public void starttimer() { //set new timer timer = new timer(); //initialize timertask's job initializetimertask(); //schedule timer, after first 1000ms timertask run every 2000ms timer.schedule(timertask, 1000, 2000); // } public void stoptimertask(view v) { //stop timer, if it's not null if (timer != null) { timer.cancel(); timer = null; } } public void initializetimertask() { timertask = new timertask() { public void run() { //use handler run toast shows current timestamp handler.post(new runnable() { public void run() { } }); } }; } /** called when activity taking focus. */ @override protected void onpause() { super.onpause(); //stop timer, if it's not null if (timer != null) { timer.cancel(); timer = null; } } /** called when activity no longer visible. */ @override protected void onstop() { super.onstop(); } /** called before activity destroyed. */ @override public void ondestroy() { super.ondestroy(); }

so have do, turn off aeroplane mode without clicking on button ?

just phone call airplanemodeoff function in run method of timertask.

you don't need provide view it. method don't utilize it, can pass null parameter. guess linked button xml feature, view parameter because link same function multiple button , check 1 called it.

android timer settings activity-lifecycle airplane

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -