【问题标题】:Google Nearby Messages API: Attempting to perform a high-power operation from a non-Activity ContextGoogle Nearby Messages API:尝试从非活动上下文执行高功率操作
【发布时间】:2020-06-02 23:21:16
【问题描述】:

在适用于 Android 的 Google Nearby Messages API 上调用 subscribe 会导致异常:

Attempting to perform a high-power operation from a non-Activity Context

我的代码:

public void subscribe(final Promise promise) {
    _messagesClient = Nearby.getMessagesClient(reactContext.getApplicationContext(), new MessagesOptions.Builder().setPermissions(NearbyPermissions.BLE).build());
    _subscribeOptions = new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .setCallback(new SubscribeCallback() {
                @Override
                public void onExpired() {
                    super.onExpired();
                    emitErrorEvent(EventType.BLUETOOTH_ERROR, true);
                }
            }).build();
    Log.d(getName(), "Subscribing...");
    if (_messagesClient != null) {
        if (_isSubscribed) {
            promise.reject(new Exception("An existing callback is already subscribed to the Google Nearby Messages API! Please unsubscribe before subscribing again!"));
        } else {
            _messagesClient.subscribe(_listener, _subscribeOptions).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Exception e = task.getException();
                    Log.d(getName(), "Subscribed!" + e.getLocalizedMessage());
                    if (e != null) {
                        _isSubscribed = false;
                        promise.reject(e);
                    } else {
                        _isSubscribed = true;
                        promise.resolve(null);
                    }
                }
            });
        }
    } else {
        promise.reject(new Exception("The Messages Client was null. Did the GoogleNearbyMessagesModule native constructor fail to execute?"));
    }
}

注意:promise 参数来自 React Native,我正在尝试为 API 创建一个包装器。

在我的OnCompleteListener 内的Log.d 事件中,它会打印:

Subscribed!2803: Attempting to perform a high-power operation from a non-Activity Context

我的AndroidManifest.xml 中确实有 API 密钥和所需的权限(BLUETOOTHBLUETOOTH_ADMIN)。

在 iOS 上,API 调用工作正常。

【问题讨论】:

    标签: java android react-native google-api google-nearby


    【解决方案1】:

    解决了!上下文

    reactContext.getApplicationContext()
    

    不是 Nearby API 的有效活动上下文!我不得不使用

    getCurrentActivity()
    

    这是来自基类ReactContextBaseJavaModule的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 2021-12-01
      • 1970-01-01
      相关资源
      最近更新 更多