设备重新启动后如何注册Geofence? [英] How to register Geofence after device restarted?

查看:156
本文介绍了设备重新启动后如何注册Geofence?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个应用程序,弹出通知在我设置的位置。
一切正常。即使在我重新启动设备后。没有问题。但我注意到,如果我关闭GPS,然后重新启动我的设备,BroadcastReceiver可能尝试登录Geofence API并因为没有GPS而出错。和Geofence通知不会再弹出,直到我用gps模式重启我的设备。
我必须使用AlarmManager吗?为了每x次推一些刷新?验证GPS模式是否开启? 解决方案 这个解决方案假设您已经存储了您想要使用的地理围栏信息,这种方式将通过重新启动设备。

首次启动时,在处理 RECEIVE_BOOT_COMPLETED 的BroadcastReceiver中,检查GPS < 已启用。如果是,继续正常,但如果没有,将其添加到您的接收器中:

  @Override 
public void onReceive (Context context,Intent intent){

//或者接收者接受的任何行为
if(intent.getAction()。equals(Intent.ACTION_BOOT_COMPLETED)){
LocationManager locationManager =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER){
context.registerReceiver(this,new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
}
else {
如果(intent.getAction()。equals(LocationManager.PROVIDERS_CHANGED_ACTION)){
if(//我们很好,继续添加geofences!
}
}

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER){
context.unregisterReceiver(this);
//我们得到了我们的GPS东西,添加我们的geofences!
}
}
}


I built an app that popup Notification in location i set. Everything works smoothly. Even after I Rebooting my device. There is no problem. But I noticed that if I turn off the GPS and then reboot my device the BroadcastReceiver probably trying to login the Geofence api and gets error because there is no GPS. and the Geofence notification are not popup any more, till i reboot my device with gps on mode. Will I have to use AlarmManager? For push some refresh every x time? To verify that a GPS mode is on?

解决方案

This solution is assuming you are already storing the geofence information you want to use in a manner that will persist through reboots of the device.

On first start, in the BroadcastReceiver that handles the RECEIVE_BOOT_COMPLETED, do a check to see if GPS is enabled. If it is, continue normally, but if not, add this to your receiver:

@Override
public void onReceive(Context context, Intent intent) {

    //Or whatever action your receiver accepts
    if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER){
            context.registerReceiver(this, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
        }
        else{
            //We are good, continue with adding geofences!
        }
    }

    if(intent.getAction().equals(LocationManager.PROVIDERS_CHANGED_ACTION)){
        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER){
            context.unregisterReceiver(this);
            //We got our GPS stuff up, add our geofences!
        }
    }
}

这篇关于设备重新启动后如何注册Geofence?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆