Android GcmListenerService和RequestLocationUpdate
使用新的GcmListenerService,我试图在收到消息时获取用户位置:Android GcmListenerService和RequestLocationUpdate,android,locationmanager,Android,Locationmanager,使用新的GcmListenerService,我试图在收到消息时获取用户位置: public class MobilAirGcmListenerService extends GcmListenerService { private static final String TAG = "MobilAir:GcmIService"; @Override public void onMessageReceived (String from, Bundle data) { final
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线程,并且没有关联的循环器,您需要获得位置更新。您可以通过创建并发布到处理程序,开始请求主活套上的位置更新:
新处理程序(Looper.getMainLooper()).post(…)
或者,如果要获取所有消息的位置更新,可以在onCreate()中注册侦听器
但我不认为这会满足你的要求。一旦您处理完消息(即在onMessageReceived()上结束),GcmListenerService将自行停止(除非另一条消息已经到达),然后您将不再获得位置更新,因为我假设侦听器将在onDestroy()中注销。onMessageReceived()在线程池上调用,以便:
1.不阻塞主线程,这样就可以直接完成长时间运行的工作,而无需启动新服务。
2.使处理一条需要很长时间的消息不会阻止处理其他(可能是实时的)消息
但是,池中的线程是原始Java线程,并且没有关联的循环器,您需要获得位置更新。您可以通过创建并发布到处理程序,开始请求主活套上的位置更新:
新处理程序(Looper.getMainLooper()).post(…)
或者,如果要获取所有消息的位置更新,可以在onCreate()中注册侦听器
但我不认为这会满足你的要求。一旦您处理完消息(即在onMessageReceived()上结束),GCMLListenerService将自行停止(除非另一条消息已经到达),然后您将不再获得位置更新,因为我假设侦听器将在onDestroy()中注销