2014-10-29 66 views
0

地理圍欄之後,沒有得到任何過渡更新儘管設置場所100英里遠, 甚至不觸發任何GPS上。Android的地理柵欄不會得到任何過渡upates

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

回答

0

管理通過爲Geofence轉換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"); 
     } 
    } 
} 
相關問題