2017-08-25 125 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); 
}