gpt4 book ai didi

java - putExtra 覆盖 extractResult 的值

转载 作者:行者123 更新时间:2023-12-01 04:39:55 25 4
gpt4 key购买 nike

我在使用 intent.putExtra 从一项服务向另一项服务提供 ResultReceiver 时遇到问题。如果不使用 putExtra,我的 Intent 处理程序会定期提供结果,但是当包含它时,除了它添加的值之外,它找不到任何内容。

如果没有 putExtramExtras->mMap->table 的值为 [0] com.google.android.location.internal.EXTRA_RELEASE_VERSION 以及 [2] com.google.android.location.internal.EXTRA_ACTIVITY_RESULT(共 3 个条目)。当我在代码中包含 putExtra 时,mExtras->mMap->table 中的唯一条目是 [2] RESULT_RECEIVER

IntentService 处理程序

protected void onHandleIntent(Intent intent) {         
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity activity = result.getMostProbableActivity();
ResultReceiver receiver = intent.getParcelableExtra(ActivityRecognitionService.RESULT_RECEIVER);
Bundle bundle = new Bundle();
bundle.putParcelable("activity", activity);
receiver.send(CODE, bundle);*/
}
}

ActivityRecognitionService.ACTIVITY = Activity; 是我当前获取数据的方式。这是 Android 开发人员页面上提到的数据传输的可能性之一,但我觉得使用 ResultReceiver 或其他回调系统来完成此操作会更简洁。

拥有 Intent 的类,ActivityRecognitionService

(也恰好是绑定(bind)到 Activity 的 Service,仅供引用)

注意,activityClient的创建和连接是在onCreate()中完成的

static final String RESULT_RECEIVER = "com.example.myApp.RESULT_RECEIVER";  

void onConnected(Bundle connectionHint) {
Intent intent = new Intent(ActivityRecognitionService.this, ActivityRecognitionIntentService.class);
intent.putExtra(ActivityRecognitionService.RESULT_RECEIVER, resultReceiver);
PendingIntent callbackIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
activityClient.requestActivityUpdates(DETECTION_INTERVAL, callbackIntent);
}

我没有收到任何错误,当包含 putExtra 时,它只是无法在 hasResult(intent) 中找到任何结果。

最佳答案

我也一直在努力解决这个问题。我相信这是因为传递到requestActivityUpdates()的pendingIntent设置了FLAG_UPDATE_CURRENT(根据我的理解,它覆盖了 Intent 中的额外内容 - 在这种情况下返回你在其中传递的内容)。

我通过将 DetectedActivity(因为它实现了 Parcelable)传递回 startService Intent 中的调用服务并在 onStartCommand 中处理它来解决这个问题。

Activity 识别 Intent 服务:

@Override
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {

ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

DetectedActivity detected = result.getMostProbableActivity();

Intent intent= new Intent(this, CallingService.class);
intent.putExtra(CallingService.EXTRA_ACTIVITY, detected);

startService(intent);
}
}

调用服务:

public int onStartCommand(Intent intent, int flags, int startId) {  

if(intent.hasExtra(CallingService.EXTRA_ACTIVITY)){

DetectedActivity result = intent.getParcelableExtra(CallingService.EXTRA_ACTIVITY);
// REST OF PROCESSING
}

return START_STICKY;
}

希望这会有所帮助,或者其他人可以提供替代方案。

关于java - putExtra 覆盖 extractResult 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16726604/

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