Android 地理围栏未触发、导致 GPS 活动或调用 IntentService

Posted

技术标签:

【中文标题】Android 地理围栏未触发、导致 GPS 活动或调用 IntentService【英文标题】:Android Geofence Not Triggering, Causing GPS Activity, or Calling IntentService 【发布时间】:2015-12-24 21:14:45 【问题描述】:

所以我遵循了我在这里找到的文档:https://developer.android.com/training/location/geofencing.html

实施本教程后,我可以看到我正在测试的地理围栏已创建并添加。我获得了成功状态。

我现在的问题是:地理围栏已添加,但我的手机没有显示 GPS 活动,即使已成功添加,地理围栏也没有触发 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,所以我没有发布,但如果有人需要,我可以发布。

关于缺少什么的任何想法?

【问题讨论】:

猜猜这让每个人都感到难过,而不仅仅是我...... 您找到解决方案了吗?我在同一条船上。 【参考方案1】:

我发现 Google 地理围栏解决方案的有趣之处在于,它在进行 GPS 民意调查时不会显示 GPS 图标,并且主要通过进行网络位置调用来降低电力成本。

至于你的问题,如果你有一个非常小的地理围栏(

【讨论】:

你刚刚救了我。我设置的半径实际上很小。 通过 Google 的 API 使用小型地理围栏并不是很有帮助。我发现使用 100m 或更大的地理围栏取得了更大的成功。 我已经将半径增加到至少 1400 米,但仍然没有触发?不过感谢您的建议! 尝试在方法buildGoogleApiClient()中调用mGoogleApiClient.connect()

以上是关于Android 地理围栏未触发、导致 GPS 活动或调用 IntentService的主要内容,如果未能解决你的问题,请参考以下文章

地理围栏未触发(未决意图和广播接收器)

未触发 Android 地理围栏广播接收器

在后台某个时间后未触发Android地理围栏?

仅使用 gps 定位模式的 Android 地理围栏监控

地理围栏 - 触发“GEOFENCE_NOT_AVAILABLE”的问题

应用程序在后台时未触发地理围栏通知