android - How to write mock gps provider affect whole system apps -


i see in market there many mock location application apply whole system. (it means when use google map, uber ...) display new location.

i have followed tutorials. here sample code:

public class mainactivity extends appcompatactivity {      locationmanager mlocationmanager;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mlocationmanager  = (locationmanager) getsystemservice(location_service);          setupmocklocations(locationmanager.gps_provider);         setupmocklocations(locationmanager.network_provider);         setupmocklocations(locationmanager.passive_provider);      }         private void setupmocklocations(final string providername) {     new thread(new runnable() {         @override         public void run() {             (; ; ) {                 mlocationmanager.addtestprovider(providername, false, //requiresnetwork,                         false, // requiressatellite,                         false, // requirescell,                         false, // hasmonetarycost,                         false, // supportsaltitude,                         false, // supportsspeed, s                         false, // upportsbearing,                         criteria.power_low, // powerrequirement                         criteria.accuracy_fine); // accuracy                  mlocationmanager.settestproviderenabled(providername, true);                 location.settime(system.currenttimemillis());                 location.setaccuracy(1);                 location.settime(system.currenttimemillis());                  method locationjellybeanfixmethod = null;                 try {                     locationjellybeanfixmethod = location.class.getmethod("makecomplete");                 } catch (nosuchmethodexception e) {                     e.printstacktrace();                 }                  if (locationjellybeanfixmethod != null) {                     try {                         locationjellybeanfixmethod.invoke(location);                     } catch (illegalaccessexception e) {                         e.printstacktrace();                     } catch (invocationtargetexception e) {                         e.printstacktrace();                     }                 }                  mlocationmanager.settestproviderlocation(providername, location);                   try {                     thread.sleep(1000);                 } catch (interruptedexception e) {                     e.printstacktrace();                 }                  log.e("print", "mocking " + providername);             }          }     }).run(); }   } 

but looks code doesn't work. (it should display samplelocation on google map instead of current location). have faked both gps_provider network_provider , passive_provider also. please tell me how this.

how write mock gps provider affect whole system apps

it affects whole system , work. conditions change result. example in case,

when call setupmocklocations(locationmanager.gps_provider); gps provider caches fake location. @ time(or later), if other app(s) makes location request gps , receives fresh gps location, fake location overridden.

your code correct, needs call setupmocklocations(...); periodically prevent above situation.

this sample works me, please consider steps

public class samplemocker {      private int mperiod = 2 * 1000;     private location mmocklocation;     private locationmanager mlocationmanager;     private handler mhandler = new handler();      public samplemocker(context context){         mlocationmanager = (locationmanager)             context.getsystemservice(context.location_service);          mmocklocation = new location(locationmanager.gps_provider);         mlocationmanager.addtestprovider(gps_provider, false, false, false,          false, false, true, true, 0, 5);         mlocationmanager.settestproviderenabled(gps_provider, true);     }      private void start(){         mhandler.post(new runnable() {             @override             public void run() {                 mmocklocation.settime(system.currenttimemillis());                  if (build.version.sdk_int >= build.version_codes.jelly_bean_mr1) {                     mmocklocation.setelapsedrealtimenanos(systemclock.elapsedrealtimenanos());                 }                  mlocationmanager.settestproviderlocation(gps_provider, mmocklocation);                  mhandler.postdelayed(this, mperiod);             }         });     } } 

this code periodically sets mock location @ each 2 sec. private int mperiod = 2 * 1000;

you should adjust period time usage. because if other app(s) may make location request lower period. mock location not appear often.

for test : while running code, open google maps , do not use high accuracy, set device mode.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -