我正在尝试使用 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)
任何帮助,将不胜感激。