android - Track user location like Google does -
i'm developing application must track user location , send remote server. know there's ton of other questions here this, unable find need.
basically i'm using fusedlocationproviderapi receive location updates. it's useful , easy use. problem need query locations , it's draining battery real quick (although maybe part of battery use can network operations send location server). however, need frequent updates while user moving.
so i'm looking google track android users (as can see in locationhistory). know if locations obtained google smartphone available somewhere? or maybe strategy implement similar?
a suggestion lower battery consumption, use activity detection determine how need determine position. in regard can recommend https://github.com/mcharmas/android-reactivelocation contributed activity detection part. library reduces lines of coded needed fused locations.
the library includes simple example of usage here: https://github.com/mcharmas/android-reactivelocation/blob/master/sample/src/main/java/pl/charmas/android/reactivelocation/sample/mainactivity.java
as wrote in comment not easy find examples of using activitydetectionapi. 1 of reasons why added library. in fact, documentation available @ time google outdated had updated google play services api, not tutorials.
i not have pointers tutorials not using library, can provide snippet of code using reactivelocation. code running in service, keeps track of current activity regardless of app being in focus or not:
private void requestfilteredactivityupdates() { reactivelocationprovider locationprovider = new reactivelocationprovider(getapplicationcontext()); filteredactivitysubscription = locationprovider.getdetectedactivity(0).doonerror(new action1<throwable>() { @override public void call(throwable throwable) { string message = "error on activitysubscription: " + throwable.getmessage(); log.e(tag, message, throwable); crashlytics.logexception(throwable); } }).onerrorreturn(new func1<throwable, activityrecognitionresult>() { @override public activityrecognitionresult call(throwable throwable) { list<detectedactivity> list = new arraylist<detectedactivity>(); list.add(new detectedactivity(detectedactivity.unknown, 0)); return new activityrecognitionresult(list, system.currenttimemillis(), systemclock.elapsedrealtime()); } }).filter(new func1<activityrecognitionresult, boolean>() { @override public boolean call(activityrecognitionresult activityrecognitionresult) { detectedactivity detectedactivity = activityrecognitionresult.getmostprobableactivity(); boolean highconfidence = detectedactivity.getconfidence() > 75; detectedactivity previousactivity = activitydetectionmodule.recent.getdetectedactivity(); boolean isnewactivity = detectedactivity.gettype() != previousactivity.gettype(); boolean hashigherconfidence = detectedactivity.getconfidence() > previousactivity.getconfidence(); return mjuststarted || (highconfidence && (isnewactivity || hashigherconfidence)); } }).subscribe(new action1<activityrecognitionresult>() { @override public void call(activityrecognitionresult activityrecognitionresult) { detectedactivity detectedactivity = activityrecognitionresult.getmostprobableactivity(); log.i(tag, "activity changed or increased in confidence:"); log.i(tag, "new: " + activitydetectionmodule.getnamefromtype(detectedactivity.gettype()) + " confidence: " + detectedactivity.getconfidence()); } }); } and in ondestroy() call
public void unsubscribeactivityupdates() { unsubscribe(filteredactivitysubscription); } private void unsubscribe(subscription subscription) { if (subscription != null && !subscription.isunsubscribed()) { log.i(tag, "unsubscribe activity updates"); try { subscription.unsubscribe(); } catch (exception e) { e.printstacktrace(); } subscription = null; } } i hope illustrates enough how use library, otherwise feel free ask.
Comments
Post a Comment