地理围栏有效,但一段时间后停止触发

Posted

技术标签:

【中文标题】地理围栏有效,但一段时间后停止触发【英文标题】:Geofence works but after a while stops triggering 【发布时间】:2017-04-10 02:50:44 【问题描述】:

我的地理围栏在开始时工作,但在一两天后突然停止触发,是谷歌方面的问题还是我的代码有问题?

在启动和启动应用程序时,我使用一个 IntentService,然后注册一个地理围栏:

public class RegisterGeoIntentService extends IntentService implements 
GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> 

private static final String TAG = "RegisterGeoIS";

private static final long TIME_OUT = 100;
protected GoogleApiClient mGoogleApiClient;
protected ArrayList<Geofence> mGeofenceList;
private PendingIntent mGeofencePendingIntent;

public RegisterGeoIntentService() 
    super(TAG);


@Override
public void onCreate() 
    super.onCreate();
    Log.i(TAG, "Creating Register Geo Intent service");
    mGeofenceList = new ArrayList<Geofence>();
    mGeofencePendingIntent = null;


@Override
protected void onHandleIntent(Intent intent) 
    buildGoogleApiClient();
    populateGeofenceList();
    mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);
    String connected = mGoogleApiClient.isConnected() ? "connected" : "disconnected";
    Log.i(TAG, "Restoring geofence - status: " + connected);

    String mode = null;
    if(intent != null) 
        mode = intent.getStringExtra(GEOFENCE_MODE);
        if(mode.equals(GEOFENCE_MODE_START)) 
            removeGeofencesButtonHandler();
            addGeofencesButtonHandler();
         else 
            removeGeofencesButtonHandler();
        
     else 
        Log.e(TAG, "No Intent data, could not start Geo");
    


@Override
public void onConnected(Bundle bundle) 
    Log.i(TAG, "Connected to GoogleApiClient");


@Override
public void onConnectionSuspended(int i) 
    Log.i(TAG, "Connection suspended");


@Override
public void onConnectionFailed(ConnectionResult result) 
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());


@Override
public void onResult(Status status)        // Not used, using await
    if(status.isSuccess()) 
        Log.i(TAG, "Geofences added");
        mGoogleApiClient.disconnect();
     else 
        Log.i(TAG, "Geofences not successful");
        String errorMessage = GeoErrorMessages.getErrorString(this, status.getStatusCode());
        Log.e(TAG, errorMessage);
    


public void addGeofencesButtonHandler() 
    if(!mGoogleApiClient.isConnected()) 
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    
    Status result = null;
    try 
        result = LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            getGeofencingRequest(),
            getGeofencePendingIntent()
        ).await(TIME_OUT, TimeUnit.MILLISECONDS);
     catch (SecurityException securityException)   // TODO Catch if manually disabled
        // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        logSecurityException(securityException);
    
    if(result != null) 
        Log.i(TAG, "Trying to add Geofences - result: " + result.toString());
     else 
        Log.i(TAG, "Trying to add Geofences - result: is null");
    


public void removeGeofencesButtonHandler() 
    if(!mGoogleApiClient.isConnected()) 
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    
    Status result = null;
    try 
        result = LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, getGeofencePendingIntent()
        ).await(TIME_OUT, TimeUnit.MILLISECONDS);
     catch (SecurityException securityException)  // TODO Catch if manually disabled
        // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        logSecurityException(securityException);
    
    if(result != null) 
        Log.i(TAG, "Trying to remove Geofences - result: " + result.toString());
     else 
        Log.i(TAG, "Trying to remove Geofences - result: is null");
    


private GeofencingRequest getGeofencingRequest() 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
    // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
    // is already inside that geofence.
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();


private PendingIntent getGeofencePendingIntent() 
    if(mGeofencePendingIntent != null) 
        return mGeofencePendingIntent;
    
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    // Removed, using Broadcast now
    // Intent intent = new Intent(this, GeoTransitionsIntentService.class);
    // return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Intent intent = new Intent("com.xyz.app.ACTION_RECEIVE_GEOFENCE");
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


private void logSecurityException(SecurityException securityException) 
    Log.e(TAG, "Invalid location permission. You need to use ACCESS_FINE_LOCATION with geofences", securityException);


public void populateGeofenceList() 
    RealmHelper realmHelper = RealmHelper.getInstance(this);
    for(Map.Entry<String, LatLng> entry : realmHelper.queryLandMarks().entrySet()) 
        mGeofenceList.add(new Geofence.Builder()
        .setRequestId(entry.getKey())
        .setCircularRegion(
            entry.getValue().latitude,
            entry.getValue().longitude,
            Constants.GEOFENCE_RADIUS_IN_METERS)
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
        .build());
    


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


还有这个:

public class GeofenceReceiver extends BroadcastReceiver 
...

@Override
public void onReceive(Context context, Intent intent) 
this.context = context;

GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if(geofencingEvent.hasError()) 
    String errorMessage = GeoErrorMessages.getErrorString(context, geofencingEvent.getErrorCode());
    Log.e(TAG, errorMessage);
    return;


int geofenceTransition = geofencingEvent.getGeofenceTransition();

if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) 

// Do stuff

...

另请参阅我的答案,我尝试并认为我已修复。但这并没有帮助。

【问题讨论】:

我已尝试为此应用禁用 DozeMode 这是不可能的 android 下,设置,电池,电池优化,所有应用...在 Nexus 6P 中选择和不优化 这并不像你认为的那样。您不能为应用禁用打盹模式 而这个commonsware.com/blog/2015/11/11/google-anti-trust-issues.html你可以手动添加权限并请求。 你可能是对的,我重新表述了这个问题...... 【参考方案1】:

请注意这实际上没有帮助

我的 GeoFence 总是被触发,但 IntentService 是问题

private PendingIntent getGeofencePendingIntent() 
    if(mGeofencePendingIntent != null) 
        return mGeofencePendingIntent;
    
    Intent intent = new Intent(this, GeoTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back     when calling
    // addGeofences() and removeGeofences().
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

改为使用 AndroidManifest 中定义的 BroadcastReceiver 为

private PendingIntent getGeofencePendingIntent() 
    if(mGeofencePendingIntent != null) 
        return mGeofencePendingIntent;
    
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    // Removed, using Broadcast now
    // Intent intent = new Intent(this, GeoTransitionsIntentService.class);
    // return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Intent intent = new Intent("com.xyz.myapp.ACTION_RECEIVE_GEOFENCE");
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

如果您自己的答案不起作用/不正确,请不要接受。等到你从其他人那里得到正确的解决方案。 正如我所提到的,我认为我已修复它,但它不是解决方案(我不能删除这个答案,因为它是故事的一部分)。 也许管理员可以帮忙【参考方案2】:

我在 Oreo 上遇到过类似的问题,使用 getApplicationContext() 解决了。

【讨论】:

以上是关于地理围栏有效,但一段时间后停止触发的主要内容,如果未能解决你的问题,请参考以下文章

Android O 地理围栏触发延迟

Android地理围栏不会触发一次位置关闭和再次打开[重复]

Android 地理围栏未触发、导致 GPS 活动或调用 IntentService

没有 requestLocationUpdates 不会触发 Android 地理围栏

应用程序在后台时未触发地理围栏通知

在打开另一个 GPS 应用程序之前,地理围栏随机无法触发