Android Location service didn't work in background -
i working on location-app should begin track statistic data based on longitude , latitude when user presses button
. stuff works when comes lock screen or put app in background service not work anymore !
i have read lot background services , broadcast receivers don't know how implement google api location listener in service , implement class in mainactivity
. can tell me short code example how implement such service or link explained ?
use fuse location service , save updated location every time
public class locationnotifyservice extends service implements locationlistener, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { locationrequest mlocationrequest; googleapiclient mgoogleapiclient; public static location mcurrentlocation; ............................. @override public void oncreate() { //show error dialog if goolgleplayservices not available if (isgoogleplayservicesavailable()) { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(background_interval); mlocationrequest.setfastestinterval(background_interval); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); //mlocationrequest.setsmallestdisplacement(10.0f); /* min dist location change, here 10 meter */ mgoogleapiclient = new googleapiclient.builder(this) .addapi(locationservices.api) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); mgoogleapiclient.connect(); } } @override public int onstartcommand(intent intent, int flags, int startid) { return super.onstartcommand(intent, flags, startid); } //check google play available or not private boolean isgoogleplayservicesavailable() { int status = googleplayservicesutil.isgoogleplayservicesavailable(getapplicationcontext()); return connectionresult.success == status; } @override public void onconnected(bundle bundle) { startlocationupdates(); } protected void startlocationupdates() { try { pendingresult<status> pendingresult = locationservices.fusedlocationapi.requestlocationupdates( mgoogleapiclient, mlocationrequest, this); } catch (illegalstateexception e) {} } @override public void onlocationchanged(location location) { //save location } ............................ }
you can current location onlocationchanged()
for more details check http://javapapers.com/android/android-location-fused-provider/ or follow official guide https://developer.android.com/training/location/index.html
Comments
Post a Comment