如何设置Android Mock GPS位置? [英] How to SET Android Mock GPS location?
问题描述
描述
我在尝试使用android模拟位置时遇到问题,我的主要目标是将android GPS设置为认为我们处于不同的位置,又名假GPS。
当前尝试次数
我目前尝试了两种不同的类似解决方案,网站:
这两个教程都是从2012年开始的,我不知道它们是否过时,或者我很难实现它们。
编码开发
首先我确定我有权限: strong>
注意:1和2都被检查,如果它们不可用我使用 requestPermissions 函数请求权限。第三个是不可行的,实际上它只能通过 AndroidManifest.xml 在调试模式下添加。
我也做了确定我在 Settings> Developer options> Allow MOCK locations 中选择了我的APP。
毕竟,我已经制作了这个功能,通过OnCreate方法或按钮的OnClick调用:
public void mockLocation(){
LocationManager lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
标准标准=新标准();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
字符串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);
位置loc =新位置(mocLocationProvider);
位置mockLocation =新位置(mocLocationProvider); //一个字符串
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();
$ / code>
如果我以这种方式进行编译和测试,我会收到 Toast :找不到位置提供商!。但是,如果我将mocLocationProvider更改为:
字符串mocLocationProvider = LocationManager.NETWORK_PROVIDER;
我得到应用程序崩溃,并且在LogCat中说它在此行崩溃:
lm.setTestProviderLocation(mocLocationProvider,mockLocation);
所以我现在没有选择,不知道如何前进。
您必须设置准确性和ElapsedRealtimeNanos值
location.setAccuracy(...);
if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN_MR1){
location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
否则会抛出IllegalArgumentException。
如果位置不完整,则抛出IllegalArgumentException
UPDATE
如果你模拟网络提供者
String mocLocationProvider = LocationManager.NETWORK_PROVIDER ;
然后您将通过网络提供商接收模拟位置,
LocationListener listener = new LocationListener(){
...
void onLocationChanged(位置位置){
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,...,listener);
而且每当你调用setTestProviderLocation方法,那么onLocationChanged回调就会被触发!
Description
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.
Current attempts
I have currently tried two different similar solutions, avaiable in both this websites:
Both tutorials are from 2012, I don't know if they are outdated or I'm having a hard time implementing them to work.
Coding development
First I make sure I have permissions:
- ACCESS_COARSE_LOCATION
- ACCESS_FINE_LOCATION
- ACCESS_MOCK_LOCATION *
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.
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();
}
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;
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.
You must set accuracy and ElapsedRealtimeNanos values
location.setAccuracy(...);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
Otherwise it throws IllegalArgumentException.
@throws IllegalArgumentException if the location is incomplete
UPDATE
If you mock Network provider
String mocLocationProvider = LocationManager.NETWORK_PROVIDER;
then you will receive mock location through Network provider,
LocationListener listener = new LocationListener(){
...
void onLocationChanged(Location location){
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, ..., listener);
And whenever you invoke setTestProviderLocation method, then onLocationChanged callback will be triggered !
这篇关于如何设置Android Mock GPS位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!