問題描述
我正在編寫一個應用程序,它利用了 android 中的位置模擬可能性.
I am writing an App which makes use of the location mocking possibility in android.
我想要實現的是模擬我的位置,而不在開發人員選項中設置允許模擬位置"標志.
What I would like to achive is to mock my location without setting the "allow mock locations" flag in the developer options.
我知道這是可能的,因為它適用于這個應用程序:https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en
I know that it is possible, because is works with this app: https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en
我嘗試了什么:
生成一個 apk,將其移至/system/app,然后重新啟動
而且我還嘗試了在清單中使用和不使用 ACCESS_MOCK_LOCATION 權限的情況.
Generate an apk, move it to /system/app, do a reboot
And I also tried it with and without the ACCESS_MOCK_LOCATION permission in the manifest.
但這一切都導致了這個例外:
RuntimeException:無法啟動 Activity:SecurityException:需要 ACCESS_MOCK_LOCATION 安全設置
But it all lead to this exception:
RuntimeException: Unable to start Activity: SecurityException: Requires ACCESS_MOCK_LOCATION secure setting
推薦答案
我已經反編譯了你提到的com.lexa.fakegps
,它的作用是這樣的:
I have decompiled com.lexa.fakegps
you mentioned in question, and what it do like this:
private int setMockLocationSettings() {
int value = 1;
try {
value = Settings.Secure.getInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION);
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, 1);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
private void restoreMockLocationSettings(int restore_value) {
try {
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, restore_value);
} catch (Exception e) {
e.printStackTrace();
}
}
/* every time you mock location, you should use these code */
int value = setMockLocationSettings();//toggle ALLOW_MOCK_LOCATION on
try {
mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, fake_location);
} catch (SecurityException e) {
e.printStackTrace();
} finally {
restoreMockLocationSettings(value);//toggle ALLOW_MOCK_LOCATION off
}
由于這些代碼的執行時間很短,其他應用很難檢測到ALLOW_MOCK_LOCATION的變化.
Because the execution time of these code is very short, other apps can hardly detect the change of ALLOW_MOCK_LOCATION.
你也需要,
1. 需要對您的設備進行 root 訪問
2. 將您的應用移動到 /system/app
或 /system/priv-app
我在我的項目中嘗試過,效果很好.
I tried it in my project and it works fine.
這篇關于使用模擬位置而不在設置中設置它的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!