Android sequence task -
i have code
public class restaurantlistfragment extends listfragment { arraylist<restaurant> restaurants; sqldatahelper datahelper; gpstracker gps; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.restaurant_list, null); } public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // uses call gps service . if not enable . display dialog user . gps = new gpstracker(getactivity()); double userlatitude = gps.getlatitude(); double userlongitude = gps.getlongitude(); // want after user enable gps service . run code below . toast.maketext(getactivity(), userlatitude + " " + userlongitude, toast.length_short).show(); datahelper = new sqldatahelper(getactivity(), "restaurantdb"); restaurants = new arraylist<restaurant>(); datahelper.opendb(); cursor cursor = datahelper.query("restaurant", new string[]{"id", "resname", "logo", "address", "latitude", "longitude"}, null , null, null, null, null); if (cursor.movetofirst()) { { restaurant restaurant = new restaurant(); restaurant.setid(cursor.getint(0)); restaurant.setresname(cursor.getstring(1)); restaurant.setlogo(cursor.getstring(2)); restaurant.setaddress(cursor.getstring(3)); restaurants.add(restaurant); } while (cursor.movetonext()); } //collections.sort(restaurants); restaurantadapter restaurantadapter = new restaurantadapter(getactivity(),restaurants); setlistadapter(restaurantadapter); }
}
i want user enable gps first . after run database query code below . user must finish enable setting gps . , datahelper must wait finish setting gps task can run . can me ???
my gpstracker class
public class gpstracker extends service implements locationlistener{ private final context mcontext; boolean isgpsenable = false; boolean isnetworkenable = false; boolean cangetlocation = false; location location; double latitude; double longitude; private static final long min_distance_for_update = 10; // 10 meters private static final long min_time_bw_update = 1000 * 60 * 1; // 1 minute protected locationmanager locationmanager; public gpstracker(context ctx) { this.mcontext = ctx; getlocation(); } public location getlocation() { try { locationmanager = (locationmanager)mcontext.getsystemservice(location_service); isgpsenable = locationmanager.isproviderenabled(locationmanager.gps_provider); isnetworkenable = locationmanager.isproviderenabled(locationmanager.network_provider); if(!isgpsenable) { showsettingsgpsalert(); } else if(!isnetworkenable) { showsettingsnetworkalert(); } else { this.cangetlocation = true; if(isnetworkenable) { locationmanager.requestlocationupdates(locationmanager.network_provider, min_time_bw_update, min_distance_for_update, this); log.d("network", "network"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } if (isgpsenable) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_update, min_distance_for_update, this); log.d("gps enabled", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } }catch (exception ex) { log.e("<<location error>>",ex.getmessage()); } return location; } public void stopusinggps(){ if(locationmanager != null){ locationmanager.removeupdates(gpstracker.this); } } public double getlatitude(){ if(location != null){ latitude = location.getlatitude(); } // return latitude return latitude; } public double getlongitude(){ if(location != null){ longitude = location.getlongitude(); } // return longitude return longitude; } public boolean cangetlocation() { return this.cangetlocation; } public boolean cangetgps() { return this.isgpsenable; } public boolean cangetnetwork() { return this.isnetworkenable; } public void showsettingsgpsalert(){ alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("gps settings"); // setting dialog message alertdialog.setmessage("gps not enabled. want go settings menu?"); // 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(); } public void showsettingsnetworkalert(){ alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("network settings"); // setting dialog message alertdialog.setmessage("network not enabled. want go settings menu?"); // on pressing settings button alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { intent intent = new intent(settings.action_network_operator_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(); } @override public ibinder onbind(intent intent) { return null; //to change body of implemented methods use file | settings | file templates. } @override public void onlocationchanged(location location) { //to change body of implemented methods use file | settings | file templates. } @override public void onstatuschanged(string provider, int status, bundle extras) { //to change body of implemented methods use file | settings | file templates. } @override public void onproviderenabled(string provider) { //to change body of implemented methods use file | settings | file templates. } @override public void onproviderdisabled(string provider) { //to change body of implemented methods use file | settings | file templates. }
}
Comments
Post a Comment