作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
即使应用程序不在后台,我也想根据用户的当前位置动态更新地理围栏列表。所以我打电话GeofencingApi.addGeofences来自服务而不是 Activity 。
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);
}
获取地理围栏请求的代码:
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
当用户进入或退出地理围栏时,它不会触发GeofenceTransitionsIntentService。在 Activity 中实现时它工作得很好,但在服务中却不起作用。
注意:这些函数是在服务中定义和调用的,该服务根据用户当前位置动态更改 mGeofenceList。
编辑:
list :
<?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>
最佳答案
如下示例: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);
关于android - 无法从 PendingIntent 触发地理围栏转换 IntentService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537914/
我是一名优秀的程序员,十分优秀!