gpt4 book ai didi

android - 带有附加功能的地理围栏 PendingIntent

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:30 25 4
gpt4 key购买 nike

问题是:

添加地理围栏的服务:

public class GeofenceService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationClient.OnAddGeofencesResultListener, LocationClient.OnRemoveGeofencesResultListener {
...

@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected");
switch (action){
case ADD:
Log.d(TAG, "onConnected ADD");
locationClient.addGeofences(geofencesToAdd, getPendingIntent(), this);
break;
case REMOVE:
Log.d(TAG, "onConnected REMOVE");
locationClient.removeGeofences(geofencesToRemove, this);
break;
}
}

private PendingIntent getPendingIntent(){
Intent intent = new Intent().setClass(this, TransitionsIntentService.class);
intent.putExtra(EXTRA_DEALS, deals);
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
...
}

如您所见,Intent 传递一些数据并启动 TransitionIntentService :

public class TransitionsIntentService extends IntentService {

...
@Override
protected void onHandleIntent(Intent intent) {
deals = (ArrayList<Deal>) intent.getSerializableExtra(GeofenceService.EXTRA_DEALS); //THIS CAN BE NULL

int transitionType = LocationClient.getGeofenceTransition(intent);

List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent); //THIS CAN BE NULL
List<String> triggeredIds = new ArrayList<String>();

for (Geofence geofence : triggeredGeofences) {
Log.d("GEO", "onHandle:" + geofence.getRequestId());
processGeofence(geofence, transitionType);
triggeredIds.add(geofence.getRequestId());
}

...
}

如果我尝试在 getPendingIntent 中放置 Extra(..., deals)方法我有 List<Geofence> triggeredGeofences = LocationClient.getTriggeringGeofences(intent) == NULL .

如果我不通过 extra 一切正常。

我怎样才能通过我的额外费用并仍然从 LocationClient 中获得额外费用? ?

最佳答案

我知道这是一个老问题,但我遇到了完全相同的症状和问题。基本上,GeofenceEvent 似乎不能与 Intent 中的 Serializable extra 共存。我找到的唯一解决方案是展平可序列化对象,并为每个字段使用一个单独的 extra 和一个简单的数据类型,就像这个简单的例子一样:

通过 Intent 传输的对象:

public class MyObject {
public String stringfield;
public int intField;

public MyObject fromIntent(Intent intent) {
stringField = intent.getStringExtra("stringField");
intField = intent.getIntExtra("intField", -1);
return this;
}

public MyObject toIntent(Intent intent) {
intent.putExtra("stringField", stringField);
intent.putExtra("intField", intField);
return this;
}
}

创建和填充 Intent :

MyObject obj = ...;
Intent intent = ...;
myObject.toIntent(intent);

从接收到的 Intent 中提取数据:

Intent intent = ...;
MyObject obj = new MyObject().fromIntent(intent);

这有点麻烦,但这是我让它工作的唯一方法。我现在可以从同一个 Intent 中提取 GeofenceEvent 数据和我的自定义数据。

关于android - 带有附加功能的地理围栏 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com