Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Mock Location changes back to Original after a few seconds

I have an android app in which I need the user to mock their current location. Below is the code I use which let's user press the green button to start mocking their location.

Below code starts faking the location when "green button" is pressed.

greenButton.setOnClickListener(new View.OnClickListener(){
     @Override
     public void onClick(View view) { 
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.addTestProvider(LocationManager.GPS_PROVIDER,
                "requiresNetwork" == "",
                "requiresSatellite" == "",
                "requiresCell" == "",
                "hasMonetaryCost" == "",
                "supportsAltitude" == "",
                "supportsSpeed" == "",
                "supportsBearing" == "",
                Criteria.POWER_LOW,
                Criteria.ACCURACY_FINE);

        Location newLocation = new Location(LocationManager.GPS_PROVIDER);
        newLocation.setLatitude(fakeLocation.getLatitude());
        newLocation.setLongitude(fakeLocation.getLongitude());
        newLocation.setAccuracy(fakeLocation.getAccuracy());
        newLocation.setTime(System.currentTimeMillis());
        newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
        lm.setTestProviderStatus(LocationManager.GPS_PROVIDER,
                LocationProvider.AVAILABLE,
                null, System.currentTimeMillis());
        lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);
      }
 });

When I place a marker on the map and press the green button. Location mocking starts. That "fake location" becomes my current location.

But after like 10-20 seconds, the mocking just ends. My "real location" becomes my current location. I have seen a lot of examples online and they use the same code I am using to mock the location. I don't know why this happens with my app. Any help would be appreciated.

like image 705
Parth Bhoiwala Avatar asked Dec 17 '16 18:12

Parth Bhoiwala


People also ask

Why does my mock location keep changing?

You most likely have your location settings configured in a way that your phone (and apps) can use both the location from the GPS sensor and the location from the cells and wi-fi.

Why my mock location is not working?

The first thing to try is going through the setting and options with your location spoofing app. If nothing there can fix the problem, you can change spoofing apps to something that works better. If that still doesn't do the trick, try turning mock location off.


1 Answers

First of all, location request will affect your mock location for appropriate location provider.

If you create and set mock location for GPS Provider and then make location request for GPS provider again, the mock one will be persisted until the GPS hardware receive fresh location. This could take 10-20 seconds or more (But after like 10-20 seconds, the mocking just ends...)

And also this question is answer of some fake location app's problem. You can see comments like "I can't get my real current location after using this app!!!" on market. Because they need make location request as you did.

And last, please loop the mocking operation in service. I suggest doing it on background thread, maybe at each 1 or 2 seconds. With this way, you can reduce affect of possible location request of other applications.

Handler mHandler;

private void loopMocking(){
    mHandler.post(mMockRunnable);
}

private Runnable mMockRunnable = new Runnable() {
    @Override
    public void run() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        }

        newLocation.setTime(System.currentTimeMillis());
        lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);

        mHandler.postDelayed(mMockRunnable, 1000); // At each 1 second
    }
};
like image 117
blackkara Avatar answered Nov 15 '22 18:11

blackkara