作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在尝试将对象传递给 GeofenceTransitionIntentService 时遇到问题。每当我使用 putExtra() 时,geofenceTransition 为 -1,因此我总是会收到错误消息,其余代码将被跳过。如果我不使用 putExtra(),通知会起作用。有什么办法可以解决吗?
这是我想放置额外内容的代码 fragment ,currentCharacter 是一个 Character 并且确实实现了可序列化。
private PendingIntent getGeofencePendingIntent() {
if (geofencePendingIntent != null) {
Log.d(TAG, "getGeofencependingintent return");
return geofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionIntentService.class);
intent.putExtra("char", currentCharacter);
Log.d(TAG, "getGeofencependingintent new");
return PendingIntent.getService(this, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
}
这是出错的代码段。这是在扩展 IntentService 的类中
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = "err";
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
this,
geofenceTransition,
triggeringGeofences
);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
Log.d(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, "error");
}
currentCharacter = intent.getSerializableExtra("char");
}
最佳答案
您可以使用“共享首选项”而不是 intent.putExtra(); 来绕过这个问题。这是另一个 StackOverflow 问题的链接。 How to bypass intent extras in Android?接受的答案解释了如何使用它。
编辑:这是处理“共享偏好”的另一个来源 https://developer.android.com/training/basics/data-storage/shared-preferences.html
关于android - 使用 putExtra() 时地理围栏事件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873755/
我是一名优秀的程序员,十分优秀!