android: how to fix gps with mock(fake) coordinates?

送分小仙女□ 提交于 2019-11-29 23:24:06

What mock locations need :

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">

Also in the android phone settings make sure you have the "Allow mock locations" checkbox ticked

locationManager.addTestProvider(mocLocationProvider, false, false,
                    false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);

. Now whenever you want to send a location, call this code:

Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(location.getLatitude());  // double 
mockLocation.setLongitude(location.getLongitude()); 
mockLocation.setAltitude(location.getAltitude()); 
mockLocation.setTime(System.currentTimeMillis()); 
locationManager.setTestProviderLocation( mocLocationProvider, mockLocation); 

.

But this works only the first time

Because you called it one time.

I have to do a service for what I want to do

You can do this, even an AsyncTask will do

do this or not: lm.requestLocationUpdates()

Yes you have to with the mocLocationProvider

I already call more times the method setLocation(latitude,longitude) but it works(location updateding) only first time for google maps

There is no method called setLocation(), use setTestLocation()

What actually I have to do in asynckTask

You can use a Thread, a TimerTask or anything you like. The idea is to inject location every second into the Framework.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!