如何设置 Android Mock GPS 位置? [英] How to SET Android Mock GPS location?
问题描述
我在尝试使用 android 模拟位置时遇到问题,我的主要目标是让 android GPS 认为我们在不同的位置,也就是假 GPS.
I'm having a issue while trying to use android mock locations, my main goal is to set the android GPS into thinking we are in a different spot, AKA fake GPS.
我目前尝试了两种不同的类似解决方案,可在这两个网站上使用:
I have currently tried two different similar solutions, avaiable in both this websites:
两个教程都是 2012 年的,我不知道它们是不是已经过时了,或者我很难实现它们.
Both tutorials are from 2012, I don't know if they are outdated or I'm having a hard time implementing them to work.
首先我确保我有权限:
- ACCESS_COARSE_LOCATION
- ACCESS_FINE_LOCATION
- ACCESS_MOCK_LOCATION *
注意:1 和 2 都被选中,如果它们不可用,我会使用 requestPermissions 函数请求许可.第三个是不可用的,实际上只能在调试模式下通过AndroidManifest.xml添加.
Note: 1 and 2 are both checked, if they are not available I ask permission using requestPermissions function. The third one is not avaiable to be asked, in fact it can only be added through AndroidManifest.xml on debug mode.
我还确保在设置>开发者选项>允许模拟位置中选择了我的应用程序.
I also made sure I have my APP selected in Settings > Developer options > Allow MOCK locations.
毕竟,我已经创建了这个函数,我尝试通过 OnCreate 方法或按钮的 OnClick 调用:
After all this, I have made this function that I tried calling through OnCreate method or OnClick of a button:
public void mockLocation(){
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_FINE );
String mocLocationProvider = lm.getBestProvider( criteria, true );
if ( mocLocationProvider == null ) {
Toast.makeText(getApplicationContext(), "No location provider found!", Toast.LENGTH_SHORT).show();
return;
}
lm.addTestProvider(mocLocationProvider, false, false,
false, false, true, true, true, 0, 5);
lm.setTestProviderEnabled(mocLocationProvider, true);
Location loc = new Location(mocLocationProvider);
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(-26.902038); // double
mockLocation.setLongitude(-48.671337);
mockLocation.setAltitude(loc.getAltitude());
mockLocation.setTime(System.currentTimeMillis());
lm.setTestProviderLocation( mocLocationProvider, mockLocation);
Toast.makeText(getApplicationContext(), "Working", Toast.LENGTH_SHORT).show();
}
如果我以这种方式编译和测试,我将收到 Toast:找不到位置提供者!".但是如果我将 mocLocationProvider 更改为:
If I compile and test this way I'll receive the Toast: "No location provider found!". But if I change mocLocationProvider to:
String mocLocationProvider = LocationManager.NETWORK_PROVIDER;
我的 APP 崩溃了,在 LogCat 中,它说在这一行中崩溃了:
I get APP crash and in LogCat it says that crashed during this line:
lm.setTestProviderLocation(mocLocationProvider, mockLocation);
所以我目前没有选择,不知道如何前进.
So I'm currently out of options and don't know how to advance.
推荐答案
必须设置第一:
允许设置中的模拟位置
第二组权限:
ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATIONACCESS_MOCK_LOCATION
ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION ACCESS_MOCK_LOCATION
第三次使用这个方法:
setMock(some_lat,some_lon,some Accuracy);
private void setMock(double latitude, double longitude, float accuracy) {
locMgr.addTestProvider (LocationManager.GPS_PROVIDER,
"requiresNetwork" == "",
"requiresSatellite" == "",
"requiresCell" == "",
"hasMonetaryCost" == "",
"supportsAltitude" == "",
"supportsSpeed" == "",
"supportsBearing" == "",
android.location.Criteria.POWER_LOW,
android.location.Criteria.ACCURACY_FINE);
Location newLocation = new Location(LocationManager.GPS_PROVIDER);
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);
newLocation.setAccuracy(accuracy);
newLocation.setAltitude(0);
newLocation.setAccuracy(500);
newLocation.setTime(System.currentTimeMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
locMgr.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locMgr.setTestProviderStatus(LocationManager.GPS_PROVIDER,
LocationProvider.AVAILABLE,
null,System.currentTimeMillis());
locMgr.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);}
这篇关于如何设置 Android Mock GPS 位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!