在Android 8.0(Oreo)上模拟位置

3

我有一个应用程序(称其为A),它运行一个服务,模拟从蓝牙GPS获取的位置,以便在另一个应用程序(称其为B)中使用:

  1. A(带有获取蓝牙GPS位置的服务)
  2. Android系统中的模拟位置
  3. B(从Android系统获取位置)

在Android 7.0上一切正常,但在Android 8.0(Oreo)上,应用程序B无法读取从蓝牙获取的位置,我认为这是由于位置模拟出现问题导致的,因为日志始终打印出以下行:

E PassiveLocationListener_FLP: isFromMockProvider, return

我使用的代码来模拟位置是:
```html

我使用的代码来模拟位置是:

```
 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权限,并且该应用程序被允许在“开发者选项”中模拟位置。
我做错了什么?
提前感谢您并祝您有美好的一天。
1个回答

0

在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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接