【问题标题】:Receive Nearby API messages on Android when screen off屏幕关闭时在 Android 上接收附近的 API 消息
【发布时间】:2017-08-25 06:10:00
【问题描述】:

我希望我的 Android 应用在屏幕关闭时接收附近的消息。理想情况下,当应用不在前台时也是如此。

这可能吗?我使用哪种策略?

【问题讨论】:

标签: android google-play-services google-nearby


【解决方案1】:

Subscribe in the background

当您的应用在后台订阅信标消息时,即使您的应用当前未处于活动状态,也会在屏幕开启事件时触发低功耗扫描。您可以使用这些扫描通知来“唤醒”您的应用程序以响应特定消息。后台订阅比前台订阅消耗更少的电量,但延迟更高,可靠性更低。

// 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    相关资源
    最近更新 更多