android - Location Updates through a serivce -


i trying location updates in background when app gets closed. im trying through service. working fine when app open, when close service seems closed.

thats code im using:

mainactivity:

public class mainactivity extends activity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     intent intent = new intent(this, myservice.class);     startservice(intent); } 

myservice:

public class myservice extends service {  public static final string broadcast_action = "hello world"; private static final int two_minutes = 1000 * 60 * 2; public locationmanager locationmanager; public mylocationlistener listener; public location previousbestlocation = null; private string provider;   @override public void oncreate() { super.oncreate(); intent = new intent(broadcast_action);       }  @override public void onstart(intent intent, int startid) {       locationmanager = (locationmanager) getsystemservice(context.location_service); listener = new mylocationlistener();     provider = locationmanager.getbestprovider(new criteria(), false); locationmanager.requestlocationupdates(provider, 0, 0, listener); }  @override public int onstartcommand(intent intent, int flags, int startid) { super.onstartcommand(intent, flags, startid);        return start_sticky; }  @override public ibinder onbind(intent intent) { return null; }  protected boolean isbetterlocation(location location, location currentbestlocation) { if (currentbestlocation == null) {     // new location better no location     return true; }  // check whether new location fix newer or older long timedelta = location.gettime() - currentbestlocation.gettime(); boolean issignificantlynewer = timedelta > two_minutes; boolean issignificantlyolder = timedelta < -two_minutes; boolean isnewer = timedelta > 0;  // if it's been more 2 minutes since current location, use new location // because user has moved if (issignificantlynewer) {     return true; // if new location more 2 minutes older, must worse } else if (issignificantlyolder) {     return false; }  // check whether new location fix more or less accurate int accuracydelta = (int) (location.getaccuracy() - currentbestlocation.getaccuracy()); boolean islessaccurate = accuracydelta > 0; boolean ismoreaccurate = accuracydelta < 0; boolean issignificantlylessaccurate = accuracydelta > 200;  // check if old , new location same provider boolean isfromsameprovider = issameprovider(location.getprovider(),         currentbestlocation.getprovider());  // determine location quality using combination of timeliness , accuracy if (ismoreaccurate) {     return true; } else if (isnewer && !islessaccurate) {     return true; } else if (isnewer && !issignificantlylessaccurate && isfromsameprovider) {     return true; } return false; }   @override public void ondestroy() {        // handler.removecallbacks(sendupdatestoui);      super.ondestroy(); log.v("stop_service", "done"); locationmanager.removeupdates(listener);         }     public static thread performonbackgroundthread(final runnable runnable) { final thread t = new thread() {     @override     public void run() {         try {             runnable.run();         } {          }     } }; t.start(); return t; }   public class mylocationlistener implements locationlistener {  public void onlocationchanged(final location loc) {     log.i("myservice", "location changed");     //        }                                }  public void onproviderdisabled(string provider) { }   public void onproviderenabled(string provider) {  }   public void onstatuschanged(string provider, int status, bundle extras) {  } } } 

can pls help?

try implementing foreground service. foreground service

foreground service displays notification , never stopped until want.

implement code snippet in service

notification notification = new notification(r.drawable.icon, gettext(r.string.ticker_text),         system.currenttimemillis()); intent notificationintent = new intent(this, exampleactivity.class); pendingintent pendingintent = pendingintent.getactivity(this, 0, notificationintent, 0); notification.setlatesteventinfo(this, gettext(r.string.notification_title),         gettext(r.string.notification_message), pendingintent); startforeground(ongoing_notification_id, notification); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -