2014-10-29 13 views
0

지오 펜스는 100 마일 떨어진 곳을 설정 했음에도 불구하고 GPS가 켜져 있어도 어떤 천이 업데이트도 발생시키지 않습니다. 는 아무 것도 트리거하지 않습니다.android 지오 펜스는 천이 업 그레 이드를 얻지 못합니다

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

도 시도 :

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="false" /> 
<service android:name=".GeofenceManager" android:exported="false" /> 

지오 펜스가 onAddGeofencesResult()로 추가됩니다

답변

0

관리 방법은 성공이라고 지오 펜스 전환 의도에 대한 새로운 클래스를 작성하여 문제를 해결하려면 및 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"); 
     } 
    } 
}