簡體   English   中英

Android Geofence無法觸發,導致GPS活動或調用IntentService

[英]Android Geofence Not Triggering, Causing GPS Activity, or Calling IntentService

因此,我遵循了在這里找到的文檔: https : //developer.android.com/training/location/geofencing.html

實施完本教程后,我可以看到已經創建並添加了我正在測試的Geofence。 我獲得成功狀態。

我現在的問題是:添加了Geofence,但是我的手機沒有顯示GPS活動,並且即使成功添加了Geofence,也不會觸發PendingIntent。

我在清單中聲明了該服務:

    <service android:name=".journeytools.TraiNapIntentService"
        android:exported="false"/>

我具有以下許可:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

添加地理圍欄的類為:

public class SplashScreen extends Activity implements View.OnClickListener, ResultCallback<Status>,
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

TextView miles, push, alarm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash_screen);

    buildGoogleApiClient();
    System.out.println("Building Google Api Client");

    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext())
            != ConnectionResult.SUCCESS) {
        //TODO Add something to post a message to the screen that the service isn't available
        System.out.println("Service not Available");
    } else {
        System.out.println("Service Available");
        miles = (TextView) findViewById(R.id.tvmiles);

        push = (TextView) findViewById(R.id.tvpush);

        alarm = (TextView) findViewById(R.id.tvalarm);

        Button button = (Button)findViewById(R.id.button);

        button.setOnClickListener(this);
    }
}

ArrayList geoFenceList;
PendingIntent geoFencePendingIntent = null;

/**
 * Provides the entry point to Google Play services.
 */
protected GoogleApiClient mGoogleApiClient;

@Override
public void onClick(View v) {

    System.out.println("Onclick pressed");

    DataGather data = new DataGather();

    data.writePreferences(this, 5, false, true);
    data.writeRecentJourney(this, "Wareham", "Poole");

    double longitude = data.getGoingToLong(getBaseContext());
    double latitude = data.getGoingToLat(getBaseContext());
    float proximity = getProximity();

    /*
     * Add the geofence work.
     */

    geoFenceList = new ArrayList();

    geoFenceList.add(
            new Geofence.Builder()

                    .setRequestId("Testing the addition")

                    .setCircularRegion(
                            latitude,
                            longitude,
                            proximity)

                    .setExpirationDuration(Geofence.NEVER_EXPIRE)

                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)

                    .build()
    );

    System.out.println("Added Geofence to Arraylist");

    LocationServices.GeofencingApi.addGeofences(
        mGoogleApiClient,
        getGeofencingRequest(),
        getGeofencePendingIntent()
    ).setResultCallback(this);

    System.out.println("Added GeoFence");

}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}

/**
 * Builds a GoogleApiClient. Uses the {@code #addApi} method to request the LocationServices API.
 */
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder geoFencingRequestBuilder = new GeofencingRequest.Builder();

    geoFencingRequestBuilder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);

    geoFencingRequestBuilder.addGeofences(geoFenceList);

    System.out.println("Returning geoFencingRequestBuilder.build()");

    return geoFencingRequestBuilder.build();
}

/**
 * Uses the users inserted information to derive the proximity setting.
 * @return Returns a float of the proximity number.
 */
private float getProximity(){

    DataGather dataGather = new DataGather();

    String[] prefs = dataGather.getPreferences(this);

    System.out.println("Returning Float" + Float.parseFloat(prefs[0]) * 1609.344);

    return (float) (Float.parseFloat(prefs[0]) * 1609.344);
}

private PendingIntent getGeofencePendingIntent(){

    Intent intent = new Intent(this, TraiNapIntentService.class);

    System.out.println("Returning PendingIntent.getSerivce");

    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

/**
 * TODO Finish this method
 * @param status
 */
public void onResult(Status status) {
    if (status.isSuccess()) {
        System.out.println("Success status received");
    } else {
        System.out.println("Non-Success status received");
    }
}

/**
 * TODO Finish this method
 * @param bundle
 */
@Override
public void onConnected(Bundle bundle) {
    System.out.println("onConnected");
}

/**
 * TODO Finish this method
 * @param i
 */
@Override
public void onConnectionSuspended(int i) {
    System.out.println("onConnectionSuspended");
}

/**
 * TODO Finish this method
 * @param connectionResult
 */
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    System.out.println("Connection Failed");
}
}

我的代碼沒有到達IntentService,所以我沒有將其發布出來,但是如果有需要的人可以。

關於缺少什么的任何想法?

我對Google的地理圍欄解決方案感興趣的地方在於,它在進行GPS輪詢時不會顯示GPS圖標,並且主要通過撥打網絡位置電話來降低電力成本。

對於您的問題,如果您的地理圍欄非常小(半徑小於50m),則定位精度可能會比您的地理圍欄大,因此它不會注冊進入事件(這是您正在偵聽的唯一事件)。 嘗試更大的地理圍欄只是為了測試您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM