简体   繁体   中英

android Geofence won't get any Transition updates

Geofence does not get any transition updates, despite setting location 100 miles away, does not trigger anything even GPS is on.

public class GeofenceManager extends IntentService implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationClient.OnAddGeofencesResultListener, LocationClient.OnRemoveGeofencesResultListener {

    private Context context;
    private LocationClient locClient;
    private PendingIntent penIntent;
    private ArrayList<Geofence> geofenceList = new ArrayList<Geofence>();
    private double dLongitude, dLatitude;
    private boolean bGeofenceEnable = false;
    private BroadcastReceiver receiverGeofence;
    /* ===========================================================================================================================
       * CONSTRUCTOR -- Checks if Google play service are available 
     *----------------------------------------------------------------------------------------------------------------------------*/
    GeofenceManager(Context context){
        super("ReceiveTransitionsIntentService");
        this.context = context;
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if (ConnectionResult.SUCCESS == resultCode) {
            bGeofenceEnable = true;
        } else {
            Log.d(GPSManager.LOG_TAG,"GEO_FENCE: ERROR google play services not available");
        }
    }
    /* ===========================================================================================================================
       * METHOD -- using hard coded values for test purpose
     *----------------------------------------------------------------------------------------------------------------------------*/
    public void setGeofence( double latitude, double longitude){
        if(bGeofenceEnable){
            dLatitude = latitude = 51.515399;
            dLongitude = longitude = -0.144313;
            locClient.connect();
        }
    }

    private PendingIntent createRequestPendingIntent() {
        if (null != penIntent) {
            return penIntent;
        } else {
            Intent intent =  new Intent(context, GeofenceManager.class);                             // Create an Intent pointing to the IntentService
            return PendingIntent.getService( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        }
    }

    @Override public void onConnected(Bundle bundle) {

        Geofence geofence = new Geofence.Builder()
                .setRequestId("123")
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT|Geofence.GEOFENCE_TRANSITION_ENTER )      // exit geo fence listener
                .setCircularRegion(dLatitude, dLongitude, 20)                                       // GPS coordinates
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
        geofenceList.add(geofence);
        penIntent = createRequestPendingIntent();
        locClient.addGeofences(geofenceList, penIntent, this);
    }

    @Override public void onDisconnected() { }
    @Override public void onConnectionFailed(ConnectionResult connectionResult) { }
    @Override public void onRemoveGeofencesByPendingIntentResult(int i, PendingIntent pendingIntent) { }
    @Override public void onAddGeofencesResult(int i, String[] strings) {
        if(i == LocationStatusCodes.SUCCESS){
            Log.d(GPSManager.LOG_TAG, GPSManager.getTime()+"  GEO_FENCE: added");
        } else {
            Log.d(GPSManager.LOG_TAG, GPSManager.getTime()+"  GEO_FENCE: add ERROR "+i);
        }
      //   locClient.disconnect();
    }

    @Override public void onRemoveGeofencesByRequestIdsResult(int i, String[] strings) {
        if(i == LocationStatusCodes.SUCCESS)
            Log.d(GPSManager.LOG_TAG, GPSManager.getTime()+"  GEO_FENCE: old Removed");
    }

    @Override protected void onHandleIntent(Intent intent) {
        int transition = LocationClient.getGeofenceTransition(intent);
        if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)){
            Log.d(GPSManager.LOG_TAG,"GEO_FENCE Transition detected");
            Toast.makeText(context, "Geo fence movement detected", Toast.LENGTH_LONG).show();
            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 1000); // 200 is duration in ms
        } else {
            Log.d(GPSManager.LOG_TAG,"GEO_FENCE Transition UPDATE");
        }
    }
}

and in Manifest file I have declared:

<service android:name="com.Utilities.GeofenceManager" android:exported="true" />

also have tried:

<service android:name="com.Utilities.GeofenceManager" android:exported="false" />
<service android:name=".GeofenceManager" android:exported="false" />

Geofence is added as onAddGeofencesResult() method is called with success

Manage to fix the problem, by creating a new class for Geofence transition Intent, and removed the IntentService code from GeofenceManager class

public class GeofenceIntentService extends IntentService {

    public GeofenceIntentService() {
        super("GeofenceIntentService");
    }


    @Override protected void onHandleIntent(Intent intent) {
        int transition = LocationClient.getGeofenceTransition(intent);
        if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)){
            Log.d(GPSManager.LOG_TAG,  "GEO_FENCE "+((transition == Geofence.GEOFENCE_TRANSITION_ENTER) ? "ENTER " : "EXIT") +" Transition detected");
            Toast.makeText(this, "Geo fence movement detected", Toast.LENGTH_LONG).show();
            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 80);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_NETWORK_LITE, 1000); // 200 is duration in ms
        } else {
            Log.d(GPSManager.LOG_TAG,"GEO_FENCE Transition UPDATE");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM