ActivityTransitionRequest 中指定的活动无效

Posted

技术标签:

【中文标题】ActivityTransitionRequest 中指定的活动无效【英文标题】:Invalid activities specified in the ActivityTransitionRequest 【发布时间】:2018-09-03 15:31:23 【问题描述】:

我正在尝试使用 ActivityRecognition。但是,当我尝试获取 DetectedActivity.TILTING 或 DetectedActivity.UNKNOWN 的转换更新时出现错误。对于所有其他活动类型,我都没有问题。

这是我创建 ActivityTransitionRequest 的代码

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(this, ActivityRecognizedService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

    Task<Void> task =
            ActivityRecognition.getClient(this).
                    requestActivityTransitionUpdates(createTransitionsRequest(), pendingIntent);

    task.addOnSuccessListener(
            new OnSuccessListener<Void>() 
                @Override
                public void onSuccess(Void result) 
                    Log.d(TAG, "success");
                    // Handle success
                
            
    );

    task.addOnFailureListener(
            new OnFailureListener() 
                @Override
                public void onFailure(Exception e) 
                    Log.d(TAG, "failure", e);
                    // Handle error
                
            
    );

    // ActivityRecognition.getClient(this).requestActivityUpdates(1000L, pendingIntent);


private ActivityTransitionRequest createTransitionsRequest() 
    List<ActivityTransition> transitions = new ArrayList<>();

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.IN_VEHICLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.IN_VEHICLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.STILL)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.STILL)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.ON_FOOT)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.ON_FOOT)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.WALKING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.WALKING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.RUNNING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.RUNNING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.ON_BICYCLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.ON_BICYCLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    // The following activity transitions will result in an exception.
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.UNKNOWN)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.UNKNOWN)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.TILTING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                        .build());

    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.TILTING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                        .build());

    return new ActivityTransitionRequest(transitions);

这是我正在使用的 IntentService。

public class ActivityRecognizedService extends IntentService 
    private static final String TAG = ActivityRecognizedService.class.getSimpleName();

    public ActivityRecognizedService() 
        super("ActivityRecognizedService");
    

    public ActivityRecognizedService(String name) 
        super(name);
    

    @Override
    protected void onHandleIntent(@Nullable Intent intent) 
        if(ActivityRecognitionResult.hasResult(intent)) 
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities( result.getProbableActivities() );
        
    

    private void handleDetectedActivities(List<DetectedActivity> probableActivities) 
        for( DetectedActivity activity : probableActivities ) 
            switch( activity.getType() ) 
                case DetectedActivity.IN_VEHICLE: 
                    Log.e( TAG, "In Vehicle: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.ON_BICYCLE: 
                    Log.e( TAG, "On Bicycle: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.ON_FOOT: 
                    Log.e( TAG, "On Foot: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.RUNNING: 
                    Log.e( TAG, "Running: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.STILL: 
                    Log.e( TAG, "Still: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.TILTING: 
                    Log.e( TAG, "Tilting: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.WALKING: 
                    Log.e( TAG, "Walking: " + activity.getConfidence() );
                    break;
                
                case DetectedActivity.UNKNOWN: 
                    Log.e( TAG, "Unknown: " + activity.getConfidence() );
                    break;
                
            
        
    

这是我得到的例外:

    com.google.android.gms.common.api.ApiException: 10: SecurityException: Invalid activities specified in the ActivityTransitionRequest! at com.google.android.gms.common.internal.zzb.zza(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zzbk.zza(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zzbl.zza(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.BasePendingResult.zzc(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.BasePendingResult.zza(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.zzm.zzc(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.zzc.zza(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.zzbo.zzb(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.zzbo.zzo(Unknown Source)
                                                                         at com.google.android.gms.common.api.internal.zzbo.onConnected(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zzac.zza(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zzn.zza(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zze.zza(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zzi.zzc(Unknown Source)
                                                                         at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.os.HandlerThread.run(HandlerThread.java:61)

任何帮助将不胜感激。

【问题讨论】:

ActivityRecognizedService 扩展了哪个类?? @SantanuSur IntentService lel IntentService 是一个服务NOT 一个activity,你是如何使用requestActivityTransitionUpdates 方法的?? @SantanuSur 我不确定你告诉我什么。我已经用 IntentService 的代码更新了我的问题。你能告诉我我应该改变什么吗? For all the other activity types I don't have problems ...你和他们一起使用相同的pendingIntent吗?我的意思是Intent intent = new Intent(this, ActivityRecognizedService.class); 【参考方案1】:

根据官方android developer document,

Transition API 支持以下活动:

IN_VEHICLE
ON_BICYCLE
RUNNING
STILL
WALKING

【讨论】:

【参考方案2】:

Transition API 仅支持以下活动: - IN_VEHICLE - ON_BICYCLE - 跑步 - 仍然 - 步行

【讨论】:

欢迎来到 ***!请看How do I write a good answer?

以上是关于ActivityTransitionRequest 中指定的活动无效的主要内容,如果未能解决你的问题,请参考以下文章