I'm trying to use ActivityRecognition. However I get an error when i'm trying to get a transition update for DetectedActivity.TILTING or DetectedActivity.UNKNOWN. For all the other activity types I don't have problems.
Here is my code to create the 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);
}
This is the IntentService I'm using.
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;
}
}
}
}
}
This is the exception i'm getting:
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)
Any help would be appreciated.
As per official android developer document,
The Transition API supports the following activities:
IN_VEHICLE ON_BICYCLE RUNNING STILL WALKING