簡體   English   中英

GcmListenerService和requestLocationUpdates

[英]GcmListenerService and requestLocationUpdates

使用新的GcmListenerService,我試圖在收到消息時獲取用戶位置:

public class MobilAirGcmListenerService extends GcmListenerService {

private static final String TAG = "MobilAir:GcmIService";


@Override
public void onMessageReceived (String from, Bundle data) {

    final String message = data.getString("appehour");

    // Post notification of received message.

       lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateTime, distance, this );

    Log.i(TAG, "Received: " + message);

    Intent intentToBroadCast = new Intent(this, MobilAirNotificationClick.class);
    sendBroadcast(intentToBroadCast);

}

}

但是當調用de locationManger時出現此錯誤:無法在未調用Looper.prepare()的線程內創建處理程序

是onMessageReceived線程?

謝謝

在線程池上調用onMessageReceived()的目的是:1.不阻塞主線程,這樣就可以直接執行長時間運行的工作,而不必啟動新服務。 2.確保處理一條需要很長時間的消息不會阻止其他(可能是實時的)消息被處理。

但是,池中的線程是原始Java線程,並且沒有關聯的循環程序,您需要獲取該循環程序來更新位置。 您可以通過創建並發布到處理程序來開始在主循環器上請求位置更新:

新的Handler(Looper.getMainLooper())。post(...)

或者,如果您想獲取所有消息的位置更新,則可以在onCreate()中注冊您的偵聽器。

但是我不認為這會做您想要的。 一旦您完成了對消息的處理(即onMessageReceived()的末尾),GcmListenerService就會自行停止(除非另一條消息已經到達),然后您將不再獲得位置更新,因為我假設監聽器將在onDestroy()中注銷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM