Google附近消息API:试图从非Activity上下文执行高功率操作
Google Nearby Messages API: Attempting to perform a high-power operation from a non-Activity Context
为 Android 在 Google 附近的消息 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?"));
}
}
Note: The promise Parameter is from React Native, I'm trying to create a wrapper for the API.
在我的 OnCompleteListener
中的 Log.d
事件中,它打印:
Subscribed!2803: Attempting to perform a high-power operation from a non-Activity Context
我的 AndroidManifest.xml
.
中确实有 API 密钥和所需的权限(BLUETOOTH
、BLUETOOTH_ADMIN
)
在 iOS 上,API 调用工作正常。
解决了!上下文
reactContext.getApplicationContext()
不是附近 API 的有效 Activity 上下文!我不得不使用
getCurrentActivity()
这是基础方法 class ReactContextBaseJavaModule
.
为 Android 在 Google 附近的消息 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?"));
}
}
Note: The promise Parameter is from React Native, I'm trying to create a wrapper for the API.
在我的 OnCompleteListener
中的 Log.d
事件中,它打印:
Subscribed!2803: Attempting to perform a high-power operation from a non-Activity Context
我的 AndroidManifest.xml
.
BLUETOOTH
、BLUETOOTH_ADMIN
)
在 iOS 上,API 调用工作正常。
解决了!上下文
reactContext.getApplicationContext()
不是附近 API 的有效 Activity 上下文!我不得不使用
getCurrentActivity()
这是基础方法 class ReactContextBaseJavaModule
.