android - location listener -
i using location listener long & lat android application.
i working following code:
public location getlastknownlocation() { location location=null; try { // location manager mlocmgr = (locationmanager) ctx.getsystemservice(context.location_service); // list providers: // list<string> providers = mlocmgr.getallproviders(); criteria criteria = new criteria(); bestprovider = mlocmgr.getbestprovider(criteria, false); location = mlocmgr.getlastknownlocation(bestprovider); if(location==null) { thread.sleep(5000); location=mlocmgr.getlastknownlocation(bestprovider); } } catch(exception e) { log.i("program", e.getmessage()); } return location; } public location updatecurrentlocation() { location mloc=null; try { mloc=getlastknownlocation(); mloclistener = new loclistener(); mlocmgr.requestlocationupdates(bestprovider, 0, 0, mloclistener); mloc=getlastknownlocation(); } catch(exception e) { log.e("program", e.getmessage()); } return mloc; }
it working absolutely fine. facing problem when switched off device , start again. getting location null. after starting google map, starts working again. please guys me out on because don't want start google map before starting application. thanking support!
your getlastknownlocation
method fail when power off , power on device.
and, see that, calling before subscribe location update lister:
mloc=getlastknownlocation(); //<<<<<<<<<<<<< mloclistener = new loclistener(); mlocmgr.requestlocationupdates(bestprovider, 0, 0, mloclistener);
why need call getlastknownlocation()
? not work when start device.
when using google map, gracefully trying listen location update. and, once gets atleast 1 location update, getlastknownlocation
start working.
i see architecture need re-thinking (if method names appropriate)
this should called once, in appropriate location, when app initiates.
mlocmgr.requestlocationupdates(bestprovider, 0, 0, mloclistener);
then, whatever update need in onlocationchanged(location location)
Comments
Post a Comment