简体   繁体   English

putExtra覆盖extractResult的值

[英]putExtra overwriting values for extractResult

I am experiencing a problem with using intent.putExtra to supply a ResultReceiver from one service to another service. 我在使用intent.putExtraResultReceiver从一个服务提供给另一服务时遇到问题。 Without using putExtra my intent handler gives me periodic results, but when it is included it cannot find anything but the value it added. 如果不使用putExtra我的意图处理程序会为我提供定期的结果,但是当包含它时,除了添加的值外,它什么也找不到。

Without the putExtra the mExtras->mMap->table has as [0] com.google.android.location.internal.EXTRA_RELEASE_VERSION and as [2] com.google.android.location.internal.EXTRA_ACTIVITY_RESULT out of 3 entries. 如果没有putExtramExtras->mMap->table在3个条目中的值为[0] com.google.android.location.internal.EXTRA_RELEASE_VERSION和[2] com.google.android.location.internal.EXTRA_ACTIVITY_RESULT When I include the putExtra in my code, the only entry in my mExtras->mMap->table is [2] the RESULT_RECEIVER . 当我包括putExtra在我的代码,在我的唯一的条目mExtras->mMap->table是[2]的RESULT_RECEIVER

IntentService handler 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; is how I am currently getting the data across. 这是我目前如何获取数据的方式。 It is one of the possibilities for data transfer mentioned on the Android developer pages, but I feel it's neater to do it using a ResultReceiver or other callback system. 这是Android开发人员页面上提到的数据传输的一种可能性,但是我觉得使用ResultReceiver或其他回调系统来进行数据传输更容易。

Class owning the intent, ActivityRecognitionService 拥有意图的类ActivityRecognitionService

(which also happens to be a Service bound to an activity, for reference) (也恰好是绑定到活动的服务,以供参考)

Note that creation and connection of activityClient is done in onCreate() 请注意, 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);
}

I don't get any errors, it simply fails to find any result in hasResult(intent) when putExtra is included. 我没有任何错误,当包含putExtra时,它根本无法在hasResult(intent)找到任何结果。

I've been struggling with this also. 我也一直在为此苦苦挣扎。 I believe its because the pendingIntent passed into requestActivityUpdates() has the FLAG_UPDATE_CURRENT set (which from my understanding overwrites the extras in the intent - giving you back what you pass in it this case). 我相信这是因为传递给requestActivityUpdates()的endingIntent具有FLAG_UPDATE_CURRENT设置(根据我的理解,该覆盖将覆盖该意图中的其他内容,在这种情况下,您将获得在其中传递的信息)。

I got around this by passing the DetectedActivity (as it implements parcelable) back to my calling service in a startService Intent and process it in onStartCommand. 我通过在StartService Intent中将DetectedActivity(因为它实现可打包)传递回我的调用服务并在onStartCommand中对其进行处理来解决此问题。

ActivityRecognition IntentService: ActivityRecognition IntentService:

@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);
     }
}

Calling Service: 致电服务:

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;
}

Hope this helps or someone else can chip in with an alternative. 希望这对您有所帮助,否则其他人可以加入其中。

I was stuck on this as well, but finally found a suitable solution. 我也坚持这一点,但最终找到了一个合适的解决方案。

In the end, I created a new BroadcastIntent from within the ActivityRecognitionService, and registered a BroadcastReceiver in my manifest. 最后,我从ActivityRecognitionService内部创建了一个新的BroadcastIntent,并在清单中注册了BroadcastReceiver。

    if (ActivityRecognitionResult.hasResult(intent)) 
    {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        String myActivity = getFriendlyName(result.getMostProbableActivity().getType());

        if(Globals.SHOW_LOG)
        {
            Log.d(TAG, "ActivityRecognitionResult: " + myActivity);
            Log.d(TAG, result.toString());
        }
        // YOUR CODE GOES HERE!
        // Store the current activity in SharedPreferences or whatever

        // Broadcast the event to the receiver
        Intent broadcastIntent = new Intent(this, ReceiverActivityRecognition.class);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra(Globals.ARG_RECEIVER_EXTRA, myActivity);
        sendBroadcast(broadcastIntent);
    }

In the manifest you need to register the BroadcastReceiver: 在清单中,您需要注册BroadcastReceiver:

    <receiver android:name=".ReceiverActivityRecognition"/>

Hope this helps someone! 希望这对某人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM