gpt4 book ai didi

android - 无法在android中模拟被动位置提供者

转载 作者:行者123 更新时间:2023-11-29 01:29:10 25 4
gpt4 key购买 nike

这是我的代码:

public class MockLocationHelper {

private String providerName ;
private Context context;
private LocationManager locationManager ;

MockLocationHelper(String providerName, Context context){
this.providerName = providerName;
this.context = context;

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

try {
locationManager.addTestProvider(providerName, true, true, true, false, false, false, false, 1, 2);
locationManager.setTestProviderEnabled(providerName, true);
}catch(SecurityException e){
Log.d(TAG, "SecurityException : MockLocationHelper()");
e.printStackTrace();
}catch(IllegalArgumentException e){
Log.d(TAG, "IllegalArgumentException : MockLocationHelper()");
e.printStackTrace();
}
}

public void startMocking(String latitude, String longitude) {

Location location = new Location(providerName);
location.setLatitude(Double.parseDouble(latitude));
location.setLongitude(Double.parseDouble(longitude));
location.setAltitude(0);
location.setAccuracy(2);
location.setTime(System.currentTimeMillis());
location.setElapsedRealtimeNanos(System.nanoTime());

try {
locationManager.setTestProviderLocation(providerName, location);
}catch(SecurityException e){
Log.d(TAG, "SecurityException : startMocking()");
e.printStackTrace();
}catch(IllegalArgumentException e){
Log.d(TAG, "IllegalArgumentException : startMocking()");
e.printStackTrace();
}
}

public void stopMocking(){

try{
locationManager.removeTestProvider(providerName);
}catch(SecurityException e){
Log.d(TAG, "SecurityException : stopMocking()");
e.printStackTrace();
}catch(IllegalArgumentException e){
Log.d(TAG, "IllegalArgumentException : stopMocking()");
e.printStackTrace();
}
}
}

这是 onCreate() 函数:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText latitude = (EditText) findViewById(R.id.latitude);
final EditText longitude = (EditText) findViewById(R.id.longitude);

Location location = new Location(LocationManager.PASSIVE_PROVIDER);
location.setLatitude(Double.parseDouble(latitude.getText().toString()));
location.setLongitude(Double.parseDouble(longitude.getText().toString()))

final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MockLocationHelper mockLocationHelper = new MockLocationHelper(LocationManager.PASSIVE_PROVIDER, this);
mockLocationHelper.startMocking(latitude.getText().toString(), longitude.getText().toString());
mockLocationHelper.stopMocking();

latitude.setText("");
longitude.setText("");
}

当运行这段代码时,它会给出IllegalAregumentException : "passive"provider unknown。 如果我们用 GPS_PROVIDER 或 NETWORK_PROVIDER 替换提供者,这段代码可以正常工作。

list 权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

Mock 选项在开发者控制台中启用,我在 Kitkat 上运行应用程序。为什么我会收到此异常以及我应该如何模拟被动位置?

最佳答案

需要先调用addTestProvider。但请注意,您不能模拟被动供应商,您必须模拟 NETWORK 或 GPS 供应商。

参见 this link有关 addTestProvider 行为方式的详细信息

    public void test1_TestCaseFoo()
{
Location location = new Location("network");
location.setLatitude(-15.83554363);
location.setLongitude(-48.01770782);
location.setTime(new Date().getTime());
location.setAccuracy(100.0f);
location.setElapsedRealtimeNanos(System.nanoTime());

LocationManager locationManager = (LocationManager) getInstrumentation().getTargetContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
locationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);

locationManager.addTestProvider(LocationManager.NETWORK_PROVIDER, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
locationManager.setTestProviderStatus(LocationManager.NETWORK_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
locationManager.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);
locationManager.setTestProviderLocation(LocationManager.NETWORK_PROVIDER, location);


mActivity = getActivity();
....

关于android - 无法在android中模拟被动位置提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com