从Android Geofence事件中未收到意图

11

所有内容都可以正确构建并在模拟器中运行,但我似乎无法让我的IntentService记录任何东西。我确定我错过了或忽略了一些基本的东西,但我对Android / Java还很陌生,在这一点上已经没有更多的想法了。

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Geofence geofence;
private PendingIntent mGeofencePendingIntent;
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LatLng latLng = new LatLng(39.7492350, -104.9913250);

    geofence = new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion(latLng.latitude, latLng.longitude, GEOFENCE_RADIUS_IN_METERS)
            .setExpirationDuration(GEOFENCE_EXPIRATION)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

private boolean checkPermission() {
    Log.i(TAG, "MainActivity.checkPermission()");
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}

private GeofencingRequest getGeofencingRequest() {
    Log.i(TAG, "MainActivity.getGeofencingRequest()");
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofence(geofence);
    return builder.build();
}

private PendingIntent getGeofencePendingIntent() {
    Log.i(TAG, "MainActivity.getGeofencePendingIntent()");
    if (null != mGeofencePendingIntent) {
        return mGeofencePendingIntent;
    }

    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "MainActivity.onConnected()");
    if (checkPermission()) {
        mGeofencePendingIntent = getGeofencePendingIntent();
        LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), mGeofencePendingIntent);
    } else {
        Log.i(TAG, "Permission not granted");
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "MainActivity.onConnectionSuspended()");
    if (null != mGeofencePendingIntent) {
        LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, mGeofencePendingIntent);
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "MainActivity.onConnectionFailed()");
}}
无论是使用模拟器还是 Android 控制台更新该设备的经度/纬度,以下服务都无法接收到意图:
public class GeofenceTransitionsIntentService extends IntentService {

public GeofenceTransitionsIntentService() {
    super(GeofenceTransitionsIntentService.class.getSimpleName());
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "GeofenceTransitionsIntentService.onHandleIntent()");

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        int errorCode = geofencingEvent.getErrorCode();
        Log.e(TAG, "Location Services error: " + errorCode);
    } else {
        Log.i(TAG, "geofencingEvent was successful");
    }
}}

清单:

<?xml version="1.0" encoding="utf-8"?>

:这是一个空段落标签,用于在HTML中创建一个空白行或者占位符。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

我一直在参考以下资源:Android 文档Google 示例


1
你有几个问题没有澄清: 1-你在真机上测试过吗? 2-你是否在指定的纬度/经度范围内? 3-Google Play服务安装了吗? 4-你尝试打开Google地图,然后更新纬度/经度吗?有时这是在模拟器上启用GPS的唯一方法。 - fernandospr
请在真实设备上运行您的应用程序,然后告诉我。 - Nick Asher
1个回答

1

在模拟器中,位置服务似乎无法完全正常工作。一旦我插入了真实设备,一切都按预期运行。


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