Detect or prevent if user uses fake location

前端 未结 4 2199
生来不讨喜
生来不讨喜 2020-12-14 02:39

I will block a user from using my app if they fake the location. So I use isFromMockProvider to check if the location is fake (follow here). But isFromMo

4条回答
  •  一个人的身影
    2020-12-14 03:25

    I use this method in my projects and it work perfectly till now:

    for api < 18

    //returns true if mock location enabled, false if not enabled.
    public static boolean isMockLocationOn(Context context) {
        if (Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
            return false;
        else
            return true;
    }
    

    For api >= 18 you should use

    location.isFromMockProvider();
    

    The point is location.isFromMockProvider is buggy and some times it will show a mocked location as its OK !!! There is a workaround in this link with full detail Location on Android: Stop Mocking Me!

    the approach is :

    • Remember the most recent location labeled as a mock

    • If a new “non-mock” reading is within 1km of the last mock, reject it.

    • Only clear the last mock location after 20 consecutive “non-mock” readings.

提交回复
热议问题