我有一个应用程序(称之为A)运行一项服务,该服务模拟从蓝牙GPS获得的位置,以便在另一个应用程序中使用它(称之为B):
一切都适用于Android 7.0,但在Android 8.0(Oreo)中,应用程序B不会读取从蓝牙获取的位置,也就是说,我认为,因为日志始终打印此行,因此无法解决该问题。
E PassiveLocationListener_FLP: isFromMockProvider, return
我用来模拟位置的代码是:
private void changeToMockLocation() {
Log.i(TAG, "changeToMockLocation()");
Location newLocation = new Location(PROVIDER_NAME);
newLocation.setLatitude(mNMEAData.getLatitude());
newLocation.setLongitude(mNMEAData.getLongitude());
newLocation.setAccuracy(mNMEAData.getAccuracy());
newLocation.setTime(System.currentTimeMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
// Set mock location.
if (ActivityCompat.checkSelfPermission(
mContext, android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
try {
mLocationManager.setTestProviderLocation(PROVIDER_NAME, newLocation);
} catch (Exception e) {
Log.e(TAG, "Error while setting test provider location: " + e);
}
}
}
问题是,我没有看到打印的异常,所以我认为这个过程是正确的,但就像Android不允许我这样做是出于某些原因我无法弄清楚。
在清单中,我声明了ALLOW_MOCK_LOCATION
权限,并允许应用程序在开发人员设置中模拟位置。
我究竟做错了什么?
在此先感谢您,祝您度过愉快的一天。
在Oreo的(三星?)开发人员设置屏幕中似乎有一个错误。我还将我的应用程序设置为模拟提供程序并具有清单中的权限。对我来说,通过将应用程序作为模拟位置提供程序删除并再次添加它来解决问题:
Developer options -> Mock location app -> No apps
Developer options -> Mock location app -> Your app
我仍然在logcat中看到这些消息:
E/PassiveLocationListener_FLP: isFromMockProvider, return
I/LocationManagerService: remove xxxxxxx
I/LocationManagerService: removeUpdates, receiver.requestedID = xxxxxxx, receiver.mIdentity.mPid = 2908, receiver.mIdentity.mUid = 1000
D/SLocation: removeGpsStatusListener
D/ListenerMonitor_FLP: removeListener, not existed listener android, xxxxxxx in mListenerIdMap
E/RequestManager_FLP: [LocationManagerService] Location remove xxxxxxx from system