2017-08-25 123 views
1

我希望我的Android应用在屏幕关闭时接收附近的消息。理想情况下,当应用程序不在前台。当屏幕关闭时,在Android上接收附近的API消息

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

+0

https://stackoverflow.com/questions/36481333/google-nearby-api-background-scan-doesn%C2%B4t-work-after-application-kill的可能重复 – Brian

回答

0

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