我正在尝试从FusedLocationProviderApi
获取模拟更新,但我似乎无法使其正常工作。这是我在android instrumentation测试中的设置方法:
locationProvider = new LocationProvider(InstrumentationRegistry.getTargetContext().getApplicationContext(), settings);
// Connect first, so that we don't receive 'true' location
locationProvider.googleApiClient.blockingConnect();
// Set mock mode, to receive only mock locations
LocationServices.FusedLocationApi.setMockMode(locationProvider.googleApiClient, true);
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
// Start receiving locations; this call will connect api client, and if it's already connected (or after it connects) it will register for location updates
locationProvider.start();
}
});
// wait until we can request location updates
while (!locationProvider.isReceivingLocationUpdates) {
Thread.sleep(10);
}
在此之后,我希望任何对LocationServices.fusedLocationApi.setMockLocation(apiClient, location)
的调用都能设置我的听众会收到的模拟位置。不幸的是情况并非如此,听众保持沉默。
我设置模拟位置的万无一失(或者我认为)方法如下所示:
private void setMockLocation(final Location location) throws Exception {
assertTrue(locationProvider.googleApiClient.isConnected());
assertTrue(locationProvider.isReceivingLocationUpdates);
final CountDownLatch countDownLatch = new CountDownLatch(1);
LocationServices.FusedLocationApi.setMockMode(locationProvider.googleApiClient, true)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
assertTrue(status.isSuccess());
LocationServices.FusedLocationApi.setMockLocation(locationProvider.googleApiClient, location)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
assertTrue(status.isSuccess());
countDownLatch.countDown();
}
});
}
});
assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
}
方法成功返回,但侦听器未收到任何位置。我真的很茫然。最糟糕的部分有时是测试会通过,但是非常随机(在相同的代码执行几次的情况下会在后续调用中通过并失败)。为完整起见,我的调试清单具有以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
我在设置中允许模拟位置。
答案 0 :(得分:0)
这是我们更新的kotlin版本。请注意,您需要在开发选项中选择该应用程序作为模拟位置提供程序,并有权在调试清单文件中进行设置。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"
tools:ignore="ProtectedPermissions" />
</manifest>
活动将在onCreate中具有带有初始化程序的val
private lateinit var fusedLocationClient: FusedLocationProviderClient
在oncreate()
中 fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.setMockMode(BuildConfig.DEBUG)
fusedLocationClient.lastLocation
.addOnSuccessListener { location : android.location.Location? ->
// Got last known location. In some rare situations this can be null.
Toast.makeText(this, location.toString(), Toast.LENGTH_SHORT).show()
}
.addOnFailureListener { Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show() }
最后是模拟位置的方法
private fun setMockLocation(location: android.location.Location) {
fusedLocationClient.setMockLocation(location)
.addOnSuccessListener { Log.d(TAG, "location mocked") }
.addOnFailureListener { Log.d(TAG, "mock failed") }
}