android - show alertdialog box for disabling gps when stop using the app -
i have application uses gps , in activity user takes action ,when pressing "get location" button ,it appears alertdialog , there user enables gps.
but, when exit app or when exit activity want able disable app.
i read must override onpause method nothing happens when press arrow or when press home button.
gpstracker gps; protected locationmanager locationmanager; boolean isgpsenabled = true; boolean isnetworkenabled = true; @override public void onpause() { super.onpause(); gps = new gpstracker(mainactivity.this); try{ locationmanager = (locationmanager) .getsystemservice(location_service); // getting gps status isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (isgpsenabled && isnetworkenabled) { gps.showsettingsalertdisable(); } }catch (exception e) { e.printstacktrace(); } } public void showsettingsalertdisable(){ alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("gps settings"); // setting dialog message alertdialog.setmessage("do want disable gps?"); // on pressing settings button alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { intent intent = new intent(settings.action_location_source_settings); mcontext.startactivity(intent); } }); // on pressing cancel button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); }
add code onbackpressed() or finish()
@override public void finish() { if(!iscalledfromalertdialog) { // show alert dialog - in onclicklistener() set variable // iscalledfromalertdialog true , call finish() // don't call super.finish(); final alertdialog.builder builder = new alertdialog.builder(this); builder.setpositivebutton(yes, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { iscalledfromalertdialog = true; finish(); } }); alertdialog alert = builder.create(); alert.show(); return; } super.finish(); }
moving code activity better option else need send message service notify activity action.
Comments
Post a Comment