我正在尝试在androidTest中执行Mock Location测试,但不幸的是,我根本没有获得OnLocationChanged的回调。我在网上搜索过很多但没有得到任何有效/有用的回复。以下是我尝试过的链接:
https://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/
Android mock location on device?
以下是我正在尝试的代码。
位置TestCase
公共类LocationTest扩展了ActivityInstrumentationTestCase2 {
Activity context;
MockLocationProvider mock;
Object lock = new Object();
public LocationTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
context = getActivity();
}
public void testLocation() throws InterruptedException {
mock = new MockLocationProvider(LocationManager.GPS_PROVIDER, context);
//Set test location
mock.pushLocation(-12.34, 23.45);
LocationManager locMgr = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
LocationListener lis = new LocationListener() {
public void onLocationChanged(Location location) {
//You will get the mock location
Log.d("LocationTest", location.getLatitude()+" "+location.getLongitude());
synchronized (lock) {
lock.notifyAll();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("LocationTest", provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.d("LocationTest", provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.d("LocationTest", provider);
}
};
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locMgr.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, lis);
synchronized (lock) {
lock.wait(60 * 1000);
}
}
@Override
protected void tearDown() throws Exception {
mock.shutdown();
super.tearDown();
}
}
模拟位置提供商 公共类MockLocationProvider {
String providerName;
Context ctx;
public MockLocationProvider(String name, Context ctx) {
this.providerName = name;
this.ctx = ctx;
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.addTestProvider(providerName, false, false, false, false, false,
true, true, 0, 5);
lm.setTestProviderEnabled(providerName, true);
}
public void pushLocation(double lat, double lon) {
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
Location mockLocation = new Location(providerName);
mockLocation.setLatitude(lat);
mockLocation.setLongitude(lon);
mockLocation.setAltitude(0);
mockLocation.setTime(System.currentTimeMillis());
mockLocation.setAccuracy(0.0f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
mockLocation.setAltitude(0.0f);
lm.setTestProviderLocation(providerName, mockLocation);
}
public void shutdown() {
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.removeTestProvider(providerName);
}
}
请告诉我如果有人有任何线索或想法在androidTest中测试LocationService。
谢谢, NISHANT