Android 无法从PendingEvent触发geofence transition IntentService

Android 无法从PendingEvent触发geofence transition IntentService,android,android-pendingintent,geofencing,android-geofence,Android,Android Pendingintent,Geofencing,Android Geofence,我想根据用户的当前位置动态更新地理围栏列表,即使应用程序不在后台。因此,我是通过服务而不是活动拨打电话 public void addGeofences() { if (!mGoogleApiClient.isConnected()) { Log.v("TAG", getString(R.string.not_connected)); return; } try { LocationServices.Geofencing

我想根据用户的当前位置动态更新地理围栏列表,即使应用程序不在后台。因此,我是通过服务而不是活动拨打电话

public void addGeofences()
{

    if (!mGoogleApiClient.isConnected()) {
        Log.v("TAG", getString(R.string.not_connected));
        return;
    }

    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                getGeofencingRequest(),
                getGeofencePendingIntent(this)
        ).setResultCallback(this); // Result processed in onResult().
    } catch (SecurityException securityException) {
        logSecurityException(securityException);
    }
}
获取PendingEvent的代码:

private PendingIntent getGeofencePendingIntent(Context c) {
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(GeofenceService.this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
获取GeofencingRequest的代码:

private  GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}
当用户进入或退出地理围栏时,它不会触发GeofenceTransitionsIntentService。 它在活动中实现时工作得非常好,但在服务中不起作用

注意:这些函数是在服务中定义和调用的,该服务根据用户的当前位置动态更改mGeofenceList。

编辑

舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.routein" >

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<permission
    android:name="com.example.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />



<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

   .....

    <service android:name=".geofencing.GeofenceTransitionsIntentService" />

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="--My Api Key--" />

    .....
</application>

.....
.....

以下示例:带代码:

使用:

并添加/更改代码,如下所示:

public class RegisterGeoIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {

protected static final String TAG = "RegisterGeoIS";

private static final long TIME_OUT = 100;
protected GoogleApiClient mGoogleApiClient;
protected ArrayList<Geofence> mGeofenceList;
private PendingIntent mGeofencePendingIntent;

public RegisterGeoIntentService() {
    super(TAG);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Creating Intent service");
    mGeofenceList = new ArrayList<Geofence>();
    mGeofencePendingIntent = null;
}

@Override
protected void onHandleIntent(Intent intent) {
    buildGoogleApiClient();
    populateGeofenceList();
    mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);
    String connected = mGoogleApiClient.isConnected() ? "connected" : "disconnected";
    Log.i(TAG, "Restoring geofence - status: " + connected);
    addGeofencesButtonHandler();
}

...

public void addGeofencesButtonHandler() {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    }

    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                // The GeofenceRequest object.
                getGeofencingRequest(),
                // A pending intent that that is reused when calling removeGeofences(). This
                // pending intent is used to generate an intent when a matched geofence
                // transition is observed.
                getGeofencePendingIntent()
        ).await(TIME_OUT, TimeUnit.MILLISECONDS);
    } catch (SecurityException securityException) {
        // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        logSecurityException(securityException);
    }
    Log.i(TAG, "Trying to add Geofences - result: " + result.toString());
}

...

从何处注册地理围栏(活动、服务等)并不重要。我想你已经通过了-对我有效-请再次执行这些步骤并检查你的代码。请发布你的清单。@MarianPaździoch你使用服务测试过吗?即使应用程序不在后台,Geofence列表是否也会用新的Geofence更新?我的目的是从places API获取附近地点的详细信息,然后在用户每次更改位置时使用该数据更新地理围栏列表,而不管应用程序是否在后台。@DavidWasser我已发布了我的清单。您没有发布整个清单。你的应用程序有任何活动吗?您如何启动
服务
?如何调用
addgeofines()
?你确定有人打电话给它吗?
public class RegisterGeoIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {

protected static final String TAG = "RegisterGeoIS";

private static final long TIME_OUT = 100;
protected GoogleApiClient mGoogleApiClient;
protected ArrayList<Geofence> mGeofenceList;
private PendingIntent mGeofencePendingIntent;

public RegisterGeoIntentService() {
    super(TAG);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Creating Intent service");
    mGeofenceList = new ArrayList<Geofence>();
    mGeofencePendingIntent = null;
}

@Override
protected void onHandleIntent(Intent intent) {
    buildGoogleApiClient();
    populateGeofenceList();
    mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);
    String connected = mGoogleApiClient.isConnected() ? "connected" : "disconnected";
    Log.i(TAG, "Restoring geofence - status: " + connected);
    addGeofencesButtonHandler();
}

...

public void addGeofencesButtonHandler() {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    }

    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                // The GeofenceRequest object.
                getGeofencingRequest(),
                // A pending intent that that is reused when calling removeGeofences(). This
                // pending intent is used to generate an intent when a matched geofence
                // transition is observed.
                getGeofencePendingIntent()
        ).await(TIME_OUT, TimeUnit.MILLISECONDS);
    } catch (SecurityException securityException) {
        // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        logSecurityException(securityException);
    }
    Log.i(TAG, "Trying to add Geofences - result: " + result.toString());
}

...
    <service android:name=".RegisterGeoIntentService" />
    Intent kickoff = new Intent(context, RegisterGeoIntentService.class);
    context.startService(kickoff);