Android Question How to detect Mock Locations?

FrankBerra

Active Member
Licensed User
Longtime User
I am trying to detect mock locations with the following code

B4X:
Sub Process_Globals
   Dim JavaObject2 As JavaObject
   Dim CheckThis As Location
End Sub

Sub Service_Create
  JavaObject2.InitializeContext
  CheckThis.Initialize
End Sub

Sub Service_Start (StartingIntent As Intent)

   CheckThis.Latitude=Latitude 'Obtained by EsLocation2 lib
   CheckThis.Longitude=Longitude 'Obtained by EsLocation2 lib

Dim isFake As Boolean = JavaObject2.RunMethod("isMockLocationOn", Array(CheckThis))
End Sub



#If JAVA
//import android.app.Service;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import android.provider.Settings;
import android.provider.Settings.Secure;
import android.test.mock.MockContext;
import android.location.Location;

public boolean isMockLocationOn(Location location) {
Context context=this;
   boolean isMock = false;
    if (android.os.Build.VERSION.SDK_INT >= 18) {
        isMock = location.isFromMockProvider();
        return isMock;
    } else {
        if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
            return false;
        else {
            return true;
        }
    } 
}
#End If

On android 4.1 the sub isMockLocationOn returns me True if the option is enable in android settings but the Fake GPS provider is not running (not a big problem though)

The most problematic thing is that on android 7.1, the sub isMockLocationOn returns me always False even if a Fake GPS provider is running.

Where i am wrong?
Should i pass to the sub another type of Location object? how?
Is there a way to know if in the settings there is a Fake GPS provider configured (even if it is not running)?

Thanks in advance for help!
 

FrankBerra

Active Member
Licensed User
Longtime User
Thanks, and how to check if
isFromMockProvider?
Which location object (and how) should I pass to inline java? Because I think CheckThis is not a valid location to check for this purpose
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
sorry, i think i haven't understood...what do you mean with "This is only relevant if you get a location from an unknown provider"?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The most problematic thing is that on android 7.1, the sub isMockLocationOn returns me always False even if a Fake GPS provider is running.
Maybe I misunderstood your first post. I understood that this code returns an incorrect value:

if (android.os.Build.VERSION.SDK_INT >= 18) {
isMock = location.isFromMockProvider();

I suggest to skip it and only check the setting.
 
Upvote 0
Top