简体   繁体   English

无法从PendingIntent触发地理围栏过渡IntentService

[英]Unable to trigger geofence transition IntentService from PendingIntent

I want to dynamically update the list of geofences according to current location of user even when app is not in background. 我想根据用户当前位置动态更新地理围栏列表,即使应用不在后台。 So I am calling GeofencingApi.addGeofences from a service instead of activity. 因此,我从服务而不是活动中调用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);
    }
}

Code to get PendingIntent: 获取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);
}

Code to get GeofencingRequest: 获取GeofencingRequest的代码:

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

It does not trigger GeofenceTransitionsIntentService when user enters or exits a geofence. 用户进入或退出地理围栏时,它不会触发GeofenceTransitionsIntentService。 It works very fine when implemented in activity but it does not work from service. 在活动中实施时,它工作得很好,但不能从服务中工作。

Note: These functions are defined and called in a service which is dynamically changing mGeofenceList according to current location of user. 注意:这些函数是在服务中定义和调用的,该服务根据用户的当前位置动态更改mGeofenceList。

Edit : 编辑

Manifest: 表现:

<?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>

Following the example: https://developer.android.com/training/location/geofencing.html with code: https://github.com/googlesamples/android-play-location/tree/master/Geofencing 遵循示例: https : //developer.android.com/training/location/geofencing.html ,代码为: https//github.com/googlesamples/android-play-location/tree/master/Geofencing

Use: 采用:

 mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);

And add/change the code as following: 并添加/更改代码,如下所示:

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

...

Add the following in AndroidManifest: 在AndroidManifest中添加以下内容:

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

Call it with: 调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM