5

我的地理柵欄是在開始工作,但後突然一兩天後觸發,是否有問題在谷歌這邊或我的代碼?地理柵欄工作,但一段時間後停止觸發

在開機和啓動應用程序,我使用的是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 
} 
... 

另請參閱我的回答是我的嘗試,我想我固定的。但它沒有幫助。

+0

*我試圖禁用這個應用程序的DozeMode *這是不可能的 –

+0

在Android下,Setti ngs,電池,電池優化,所有應用程序...選擇並不要在Nexus 6P中優化 – powder366

+0

這不符合您的想法。您無法禁用應用程序的打盹模式 –

回答

0

請注意,這實際上確實沒有幫助

我的柵欄總是觸發,但IntentService改爲使用AndroidManifest定義的廣播接收器的問題

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); 
} 

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); 
} 
+0

這確實沒有奏效。 – powder366

+0

如果工作不正常或不正確,請不要接受您自己的答案。等到你從別人那裏收到正確的解決方案。 – james

+0

正如我所提到的,我認爲我已經解決了這個問題,但這不是解決方案(我不能刪除這個答案,只是說它是故事的一部分)。 – powder366