Android Question How to detect Mock Locations with newer android version (6.0+)

Cnrez

Member
Licensed User
Longtime User
hi all,

i want to block my users from using Mock Location function in android,
so first i need to detect it.

for earlier version of android, it is easy.
B4X:
Dim p As Phone
If p.GetSettings("mock_location") = "0" Then Return False Else Return True

but if you running android 6.0+ always return False
question, how do i detect mock location using android 6.0+ ?

regards
 
Last edited:

Cnrez

Member
Licensed User
Longtime User
Try this:
B4X:
Dim jo As JavaObject = Location1
Dim IsMock As Boolean = jo.RunMethod("isFromMockProvider", Null)
It will work on Android 4.4+.

thank you Erel for your help, it is working !
I added this in Tracker service module (Android Tutorial Background location tracking)

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    Dim jo As JavaObject = Location1
    Dim IsMock As Boolean = jo.RunMethod("isFromMockProvider", Null)
    
    If IsMock = False Then
        If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
            Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
            n.Notify(nid)
            LastUpdateTime = DateTime.Now
            gpslat = Location1.Latitude
            gpslong = Location1.Longitude
            CallSubDelayed2(Main,"Update_Location",Location1)
        End If
    Else
        CallSubDelayed2(Main,"CheckMock_Location",IsMock)
    End If        
    
End Sub

and add new Sub in Main module to check
B4X:
Sub CheckMock_Location(ismock As Boolean)
    
    If ismock = True Then
        btnsave.Enabled =False
        Msgbox("Please do not fake your location","Warning !")
        ExitApplication
    End If
    
End Sub
 
Upvote 0
Top