其他分享
首页 > 其他分享> > android-当设备处于深度睡眠(打ze)模式时,Geofence待处理的意图触发时间太晚

android-当设备处于深度睡眠(打ze)模式时,Geofence待处理的意图触发时间太晚

作者:互联网

我需要通知用户在指定地点附近.我为此使用Geofencing API.当我在具有模拟位置的Android模拟器上测试应用程序时,一切正常.具有模拟位置的真实设备也是如此.但是当我走路并且手机处于深度睡眠模式时,Geofence将在5-10分钟后触发.如果我在地理围栏半径内并且解锁了手机,请打开任何使用我的地理围栏触发位置的应用程序. (Android 5.1,Motorolla moto G 1代)

以下是我注册地理围栏的方式:

  public void registerLocation(RegisterAlarmRequestModel data) {
    if (isLocationDetectionAllowed() && isConnected) {
        GeofencingRequest geofencingRequest = prepareGeofencingRequest(prepareGeofence(data));
        PendingIntent pendingIntent = prepareIntent(data.getId());
        PendingResult<Status> result = GeofencingApi.addGeofences(
                googleApiClient, geofencingRequest, pendingIntent);
        Status status = result.await();
        if (status.isSuccess())
            Log.d("Location", "Geofence " + data.getId() + " has been registered");
    }
}

//preparing Geofence Pending Intent which will be triggered 
private PendingIntent prepareIntent(int alarmId) {
    Intent intent = new Intent(context, LocationRingingService.class);
    intent.putExtra(LocationRingingService.KEY_ALARM_ID, alarmId);
    return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

private GeofencingRequest prepareGeofencingRequest(Geofence geofence) {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofence(geofence);
    return builder.build();
}

private Geofence prepareGeofence(RegisterAlarmRequestModel data) {
    Geofence geofence = new Geofence.Builder()
            .setRequestId(String.valueOf(data.getId()))
            .setCircularRegion(data.getLatitude(), data.getLongitude(), data.getRadius())
            .setLoiteringDelay(100)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .build();
    return geofence;
}

为了接收意图,我正在使用IntentService:

@Override
protected void onHandleIntent(Intent intent) {
    Log.d("Location", "accepted intent: " + intent.toString());
    //database request
}

这是我在清单中注册服务的方式:

<service
        android:name=".plugin.delivery.ringing.location.service.LocationRingingService"
        android:enabled="true"
        android:exported="true" />

更新:我需要抓住用户刚进入地理围栏时的尽可能准确的时刻.我有一个想法:以大于所需的半径注册地理围栏(例如,如果需要100m半径,则以200-300m半径注册地理围栏).当用户进入较大半径的Geophence时-请通过位置查询开始服务,以提高地理围栏精度.并且当用户刚输入时-禁用位置服务.

解决方法:

为了改善它,让我们执行一些检查

1)使用广播接收器而不是服务即可轻松触发它.并使用意图过滤器设置优先级.

例如

    <receiver
        android:name=".youpackage.GeoReceiver"
        android:exported="true">
        <intent-filter android:priority="999">
            <action android:name="yourpackage.ACTION_RECEIVE_GEOFENCE" />
        </intent-filter>
    </receiver>

您的未决意图将是:

Intent intent = new Intent("yourpackage.ACTION_RECEIVE_GEOFENCE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                youractivity,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

2)当您的GPS进入睡眠模式时,我们需要在创建Geofence时将其唤醒.创建地理围栏后,您可以开始对GPS进行ping操作,直到获得ENTER转换.这将必须有助于触发它.

public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks,    GoogleApiClient.OnConnectionFailedListener, LocationListener {

    GoogleApiClient mGoogleApiClient;
    LocationRequest locationRequest;

    public GPSService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
     // TODO: Return the communication channel to the service.
     throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
      super.onCreate();


     mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

     mGoogleApiClient.connect();
  }

   @Override
   public void onLocationChanged(Location location) {

     Utility.ReadAndWriteData(this, Utility.readFileName(this), "Still Geofence is not triggered!!!");

    }

    @Override
    public void onConnected(Bundle bundle) {

        locationRequest = LocationRequest.create();
      locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setFastestInterval(1000);
        locationRequest.setInterval(2000);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this);

    }

   @Override
   public void onConnectionFailed(ConnectionResult connectionResult)   {

  }

     @Override
     public void onConnectionSuspended(int i) {

     }

     @Override
     public void onDestroy() {
       super.onDestroy();

        if(mGoogleApiClient.isConnected()) {

     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

       }
 } 

而且,当您进行ENTER转换时,请不要忘记立即停止此服务,否则会导致电池电量耗尽.此服务仅用于将GPS从睡眠模式唤醒.

标签:android-geofence,google-maps,location,android
来源: https://codeday.me/bug/20191026/1938184.html