android Geofence不会获得任何Transition更新

问题描述 投票:0回答:1

地理围栏没有获得任何转换更新,尽管设置位于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");
        }
    }
}

在Manifest文件中我声明:

<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()方法成功调用

android geo geofencing android-geofence
1个回答
0
投票

通过为Geofence transition Intent创建新类来管理以解决问题,并从GeofenceManager类中删除IntentService代码

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");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.