从PendingIntent无法触发地理围栏转换IntentService

3

我希望能够根据用户当前位置动态更新地理围栏列表,即使应用程序不在后台运行。因此,我正在从服务而不是活动中调用GeofencingApi.addGeofences

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);
    }
}

获取 PendingIntent 的代码:

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>


无论您从哪里注册地理围栏(Activity、Service等),都没有关系。我假设您已经阅读了https://developer.android.com/intl/ja/training/location/geofencing.html - 它对我有效 - 请再次按照这些步骤检查您的代码。 - Marian Paździoch
请发布您的清单。 - David Wasser
你没有发布完整的清单文件。你的应用程序有任何活动吗?你如何启动ServiceaddGeoFences()方法是如何被调用的?你确定它已经被调用了吗? - David Wasser
解决了吗?你能看一下这个问题吗? - Skizo-ozᴉʞS ツ
@Skizo 无法解决,所以我改变了策略。现在只有当用户启动应用程序时,我才会更新地理围栏列表。 - Tushar Kathuria
显示剩余4条评论
1个回答

0

根据以下示例:https://developer.android.com/training/location/geofencing.html,使用代码:https://github.com/googlesamples/android-play-location/tree/master/Geofencing

用途:

 mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);

并按照以下方式添加/更改代码:

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());
}

...

在 AndroidManifest 中添加以下内容:

    <service android:name=".RegisterGeoIntentService" />

使用以下方式调用:

    Intent kickoff = new Intent(context, RegisterGeoIntentService.class);
    context.startService(kickoff);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接