Android 使用putExtra()时地理围栏事件出错
我在尝试将对象传递到GeofenceTransitionIntentService时遇到问题。每当我使用putExtra()时,geofenceTransition是-1,因此我总是会得到一个错误,并跳过其余代码。如果不使用putExtra(),则通知确实有效。 有什么办法解决这个问题吗Android 使用putExtra()时地理围栏事件出错,android,android-intent,intentservice,geofencing,Android,Android Intent,Intentservice,Geofencing,我在尝试将对象传递到GeofenceTransitionIntentService时遇到问题。每当我使用putExtra()时,geofenceTransition是-1,因此我总是会得到一个错误,并跳过其余代码。如果不使用putExtra(),则通知确实有效。 有什么办法解决这个问题吗 这是一段代码,我想把额外的,currentCharacter是一个字符,并实现可序列化 private PendingIntent getGeofencePendingIntent() { if (ge
这是一段代码,我想把额外的,currentCharacter是一个字符,并实现可序列化
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");
}
@覆盖
受保护的手部内容无效(意图){
GeofencingEvent GeofencingEvent=GeofencingEvent.fromIntent(intent);
if(geofencingEvent.hasError()){
字符串errorMessage=“err”;
Log.e(标签、错误消息);
回来
}
//获取转换类型。
int geofenceTransition=geofencingEvent.getGeofenceTransition();
//测试报告的转换是否有意义。
如果(geofenceTransition==Geofence.Geofence\u TRANSITION\u输入||
geofenceTransition==Geofence.Geofence_TRANSITION_EXIT){
//获取已触发的地理围栏。一个事件可以触发多个地理围栏。
List triggeringGeofences=geofencingEvent.getTriggeringGeofences();
//以字符串形式获取转换详细信息。
字符串geofenceTransitionDetails=getGeofenceTransitionDetails(
这
地球围栏过渡,
触发地理围栏
);
//发送通知并记录转换详细信息。
发送通知(geofenceTransitionDetails);
Log.d(标签、geofenceTransitionDetails);
}否则{
//记录错误。
Log.e(标记“错误”);
}
currentCharacter=intent.getSerializableExtra(“char”);
}
您可以通过使用“共享首选项”而不是intent.putExtra();,绕过该问题;。这是另一个StackOverflow问题的链接。
公认的答案解释了如何使用它
编辑:这里是处理“共享首选项”的另一个来源
好的,我通过将对象转换为Json字符串并像这样传递来修复它。
在intentservice中,我从Json字符串创建了一个新对象。currentCharacter的类型是什么?currentCharacter是一个字符,并且实现了可序列化。哦,抱歉,没有看到。让我们看看intent和服务之间的数据共享