timer - Android Try Getting location for X seconds without freezing the app -
what trying gps location 2 providers, first 1 gps accurate, second 1 agps combination of gps , network. doing because agps can location in tall buildings when normal gps takes more time get. want try getting location first provider(gps) 10 seconds, if in 10 seconds location!=null, break timed loop , take result main thread, main activity. else ill take location second provider(agps) if available. if none of provider able location, return null after 10 seconds. problem facing is, when timed loop, app freezes 10 seconds im not able location main activity.
here trying location on homeactivity class extends activity:
button btnredcross = (button) this.findviewbyid(r.id.btnredcross); btnredcross.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { outrequestsdatabasehandler db =new outrequestsdatabasehandler(); outrequest outreq = new outrequest(); outreq.setname("red cross"); //todo message localuser db calendar cal = calendar.getinstance(); outreq.setdate(cal.gettimeinmillis()); outreq.setmessage("my message"); outreq.settype("redcross"); //outreq.setlongitude(12.123456); //outreq.setlatitude(12.123456); db.addoutrequest(homeactivity.this, outreq); //here trying location gpstracker locationtracker=new gpstracker(homeactivity.this); location=locationtracker.getlocation(); log.i("locationgetter","result: longitude:"+location[0]+" latitude:"+location[1]); } }); }
this gpstracker class 2 providers try location:
public class gpstracker{ context con; locationmanager locmgr; private double longgps; private double latgps; private double longnetwork; private double latnetwork; private locationlistener gpslocationlistener; private locationlistener networklocationlistener; public gpstracker(final context context){ con = context; locmgr = (locationmanager) context .getsystemservice(context.location_service); locationprovider high = locmgr.getprovider(locmgr.getbestprovider( createfinecriteria(), true)); locationprovider low = locmgr.getprovider(locmgr.getbestprovider( createcoarsecriteria(), true)); //get location gps gpslocationlistener = new locationlistener() { @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { alertdialog.builder alertdialogbuilder = new alertdialog.builder( context); alertdialogbuilder .setmessage( "please enable gps , network accurate result") .setcancelable(false) .setpositivebutton("enable gps", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { intent callgpssettingintent = new intent( android.provider.settings.action_location_source_settings); context.startactivity(callgpssettingintent); } }); alertdialogbuilder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { dialog.cancel(); } }); alertdialog alert = alertdialogbuilder.create(); alert.show(); } @override public void onlocationchanged(location location) { longgps = location.getlongitude(); latgps = location.getlatitude(); //log.i("locationgetter", "gps: longitude:" + longgps+ " latitude:" + latgps); } }; locmgr.requestlocationupdates(high.getname(), 0, 0f,gpslocationlistener); //get location gps + network networklocationlistener=new locationlistener() { @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onlocationchanged(location location) { longnetwork = location.getlongitude(); latnetwork = location.getlatitude(); //log.i("locationgetter", "network: longitude:"+ longnetwork + " latitude:" + latnetwork); } }; locmgr.requestlocationupdates(low.getname(), 0, 0f,networklocationlistener); } public static criteria createfinecriteria() { criteria c = new criteria(); c.setaccuracy(criteria.accuracy_fine); c.setaltituderequired(false); c.setbearingrequired(false); c.setspeedrequired(false); c.setcostallowed(true); c.setpowerrequirement(criteria.power_high); return c; } public static criteria createcoarsecriteria() { criteria c = new criteria(); c.setaccuracy(criteria.accuracy_coarse); c.setaltituderequired(false); c.setbearingrequired(false); c.setspeedrequired(false); c.setcostallowed(true); c.setpowerrequirement(criteria.power_high); return c; } public double[] getlocation() { double location[] = new double[2]; calendar cal = calendar.getinstance(); long endtime = cal.gettimeinmillis() + 10000; while (calendar.getinstance().gettimeinmillis() < endtime) { if (longgps != 0 && latgps != 0) { location[0] = longgps; location[1] = latgps; log.i("locationgetter", "gps: longitude:" + location[0] + " latitude:" + location[1]); break; } else if (longnetwork != 0 && latnetwork != 0) { location[0] = longnetwork; location[1] = latnetwork; log.i("locationgetter", "network: longitude:" + location[0] + " latitude:" + location[1]); } } locmgr.removeupdates(networklocationlistener); locmgr.removeupdates(gpslocationlistener); networklocationlistener = null; gpslocationlistener = null; return location; } }
isn't multithreading problem. instead of doing work on main thread, 1 create second thread doesn't matter if thread idle 10 seconds.
incidentally, instead of relying on single provider, think it's better use providers , trust them according accuracy using kalman filter. see my answer here simple kalman filter seems work in context of android location providers.
Comments
Post a Comment