android Geofence не будет получать обновления Transition

Geofence не получает никаких обновлений перехода, несмотря на то, что местоположение установлено на расстоянии 100 миль, ничего не запускает, даже если GPS включен.

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

и в файле манифеста я заявил:

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

также пробовали:

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

Геозона добавляется при успешном вызове метода onAddGeofencesResult ()


person iUK    schedule 29.10.2014    source источник


Ответы (1)


Успешно решить проблему, создав новый класс для Intent перехода Geofence и удалив код IntentService из класса GeofenceManager.

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");
        }
    }
}
person iUK    schedule 30.10.2014