【发布时间】:2017-08-25 06:10:00
【问题描述】:
我希望我的 Android 应用在屏幕关闭时接收附近的消息。理想情况下,当应用不在前台时也是如此。
这可能吗?我使用哪种策略?
【问题讨论】:
标签: android google-play-services google-nearby
我希望我的 Android 应用在屏幕关闭时接收附近的消息。理想情况下,当应用不在前台时也是如此。
这可能吗?我使用哪种策略?
【问题讨论】:
标签: android google-play-services google-nearby
当您的应用在后台订阅信标消息时,即使您的应用当前未处于活动状态,也会在屏幕开启事件时触发低功耗扫描。您可以使用这些扫描通知来“唤醒”您的应用程序以响应特定消息。后台订阅比前台订阅消耗更少的电量,但延迟更高,可靠性更低。
// Subscribe to messages in the background.
private void backgroundSubscribe() {
Log.i(TAG, "Subscribing for background updates.");
SubscribeOptions options = new SubscribeOptions.Builder()
.setStrategy(Strategy.BLE_ONLY)
.build();
Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options);
}
private PendingIntent getPendingIntent() {
return PendingIntent.getBroadcast(this, 0, new Intent(this, BeaconMessageReceiver.class),
PendingIntent.FLAG_UPDATE_CURRENT);
}
【讨论】: