2013-06-11 90 views
5

我已經開始與最後位置服務在Android開發Android地理圍欄功能:地理圍欄!模擬位置提供者有沒有任何已知的問題?以下示例(https://developer.android.com/training/location/geofencing.html)即使當前位置位於地理圍欄內,我的意圖服務也不會被解僱。我使用FakeGPS android應用程序作爲模擬位置提供程序,如果模擬路線,我會在Google地圖應用程序中看到位置更改,因此模擬位置提供程序運行良好。有任何想法嗎 ?故作位置提供

謝謝。 Paolo。

回答

3

我想永遠得到這個工作。多麼痛苦的Google!既然它說geofences可以很容易地使用mock進行測試。

的魔術是在傳遞給setMockLocation位置使用提供者名稱「網絡」。

Location location = new Location("network"); 
    location.setLatitude(latitude); 
    location.setLongitude(longitude); 
    location.setTime(new Date().getTime()); 
    location.setAccuracy(3.0f); 
    location.setElapsedRealtimeNanos(System.nanoTime()); 

    LocationServices.FusedLocationApi.setMockLocation(_googleApiClient, location); 
+2

唐的一個完整的例子您是否必須先調用LocationServices.FusedLocationApi.setMockMode(_googleApiClient,true)? –

+0

您是否有鏈接顯示LocationServices.GeofencingApi實際使用LocationServices.FusedLocationApi進行嘲諷?我沒有看到任何證據表明這是真的。 – yiati

0

確保在您的手機上啓用模擬位置。選擇設置 - >開發人員選項 - >「允許模擬位置」。

0

LocationServices.FusedLocationApi.setMockMode(googleApiClient,真) 需要設置模擬位置之前使用。如果你的應用程序是在前臺,但是當應用程序在後臺,這IntentService是永遠called.So我們需要使用廣播,接收器,而不是意圖服務的

1

在提到示例中使用實際上意圖服務的工作好。

我發現這個博客在獲得解決方案的幫助。

http://davehiren.blogspot.in/2015/01/android-geofence-stop-getting.html

+0

歡迎您訪問解決方案的鏈接,但請確保您的答案在沒有它的情況下很有用:[在鏈接附近添加上下文](// meta.stackexchange.com/a/8259),以便您的同行用戶瞭解它是什麼以及爲什麼它在那裏,然後引用您鏈接的頁面中最相關的部分,以防目標頁面不可用。 [僅僅是一個鏈接的答案可能會被刪除。](// stackoverflow.com/help/deleted-answers) –

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分並提供該鏈接供參考。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [評分](/審查/低質職位/ 15725231) – Serenity

+0

你不應該,除非,直到你有問題及其解決方案的清晰的認識Downvote答案。 @Serenity –

1

可以使用廣播接收器,而不是活性這樣

public class GeofenceReceiver extends BroadcastReceiver 
implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    ResultCallback<Status>{ 

GoogleApiClient mGoogleApiClient; 
PendingIntent mGeofencePendingIntent ; 
Context mContext; 

@Override 
public void onReceive(Context context, Intent intent) { 
    mContext = context; 
    mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
      .addOnConnectionFailedListener(this) 
      .addConnectionCallbacks(this) 
      .addApi(LocationServices.API) 
      .build(); 

    mGoogleApiClient.connect(); 
} 



@Override 
public void onConnected(@Nullable Bundle bundle) { 
    try { 
     LocationServices.GeofencingApi.addGeofences(
       mGoogleApiClient, 
       // The GeofenceRequest object. 
       getGeofencingRequest(), 
       getGeofencePendingIntent() 
     ).setResultCallback(this); // Result processed in onResult(). 
    } catch (SecurityException securityException) { 
     Log.i(getClass().getSimpleName(),securityException.getMessage()); 
    } 
} 

// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. 
@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

/** 
* Runs when the result of calling addGeofences() and removeGeofences() becomes available. 
* Either method can complete successfully or with an error. 
* 
* Since this activity implements the {@link ResultCallback} interface, we are required to 
* define this method. 
* 
* @param status The Status returned through a PendingIntent when addGeofences() or 
*    removeGeofences() get called. 
*/ 
@Override 
public void onResult(@NonNull Status status) { 
    if (status.isSuccess()) { 
     Log.i(getClass().getSimpleName(),"Success"); 
    } else { 
     // Get the status code for the error and log it using a user-friendly message. 
     Log.i(getClass().getSimpleName(),getErrorString(status.getStatusCode())); 
    } 
} 

private GeofencingRequest getGeofencingRequest() { 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL); 
    builder.addGeofences(getGeofecne()); 
    return builder.build(); 
} 

private List<Geofence> getGeofecne(){ 
    List<Geofence> mGeofenceList = new ArrayList<>(); 

    //add one object 
    mGeofenceList.add(new Geofence.Builder() 
      // Set the request ID of the geofence. This is a string to identify this 
      // geofence. 
      .setRequestId("key") 

      // Set the circular region of this geofence. 
      .setCircularRegion(
        25.768466, //lat 
        47.567625, //long 
        50) // radios 

      // Set the expiration duration of the geofence. This geofence gets automatically 
      // removed after this period of time. 
      //1000 millis * 60 sec * 5 min 
      .setExpirationDuration(1000 * 60 * 5) 

      // Set the transition types of interest. Alerts are only generated for these 
      // transition. We track entry and exit transitions in this sample. 
      .setTransitionTypes(
        Geofence.GEOFENCE_TRANSITION_DWELL) 
      //it's must to set time in millis with dwell transition 
      .setLoiteringDelay(3000) 
      // Create the geofence. 
      .build()); 

    return mGeofenceList; 

} 

private PendingIntent getGeofencePendingIntent() { 
    // Reuse the PendingIntent if we already have it. 
    if (mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    Intent intent = new Intent(mContext, GeofenceTransitionsIntentService.class); 
    return PendingIntent.getService(mContext, 0, intent, PendingIntent. 
      FLAG_UPDATE_CURRENT); 
} 

}

看看我的回購,有使用地理圍欄 https://github.com/3zcs/Geofence