我必须在出口处更新
geoFence的信息。为此,我需要目前的位置在这一点。
,当我的应用程序关闭时,,我没有onLocationChanged的侦听器。为了更新geoFence,我需要我的位置。他们有没有办法在Geofence.GEOFENCE_TRANSITION_EXIT找到当前的位置?
public void broadcastUpdateGeoFences() {
MainActivity.isGeoFenceAdded = false;//It tells to update geoFence
Intent intent = new Intent(Constants.RECEIVER_GEOFENCE);
intent.putExtra("done", 1);
sendBroadcast(intent);
}广播接收机在MainActivity中的应用
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
int resultCode = bundle.getInt("done");
if (resultCode == 1) {
if(!MainActivity.isGeoFenceAdded){
updateGeoFencesOnCurrentExit();//I need current location here
}
}
}
}
};每件事都很正常。但是,当应用程序关闭时,无法找到在出口通过currentLocation的方法。
private void updateGeoFencesOnCurrentExit(Location currentLocation){
locationHandler.updateGeoFences(currentLocation);
}发布于 2017-03-08 05:43:42
您可以在广播中实现LocationListener接口,并使用以下覆盖方法onLocationChanged
@Override
public void onLocationChanged(Location location) {
location.getLongitude();
location.getLatitude();
}发布于 2017-03-08 18:39:30
我不建议在IntentService或BroadcastReceiver中使用IntentService或BroadcastReceiver,因为一旦函数(onHandleIntent或onReceive)退出,就可以销毁它们。一旦您得到geofence事件,我将使用带有PendingIntent的LocationManager请求一个更新。我有示例应用程序演示如何使用github上的PendingIntent (https://github.com/pablobaxter/AndroidLocationExamples)请求位置更新。
编辑:
IntentService并没有错。您只是在后台线程中运行onHandleIntent逻辑,而在主线程上运行onReceive。
https://stackoverflow.com/questions/42549449
复制相似问题