Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The first transition is always the same - Activity Recognition API - Activity Transition

I use activity recognition api and activity transitions. When I first run the app, the first transition is always the. In this app it's WALKING-ENTER. When I tried with only IN_VEHICLE-ENTER and IN_VEHICLE-EXIT in transitions, it was IN_VEHICLE-ENTER. I thought about ignoring the first transition but a device I've tested on didn't have such a problem. The devices that had these problem were Android 8.1, and the device didn't have the problem was 6.0.

MainActivity extends AppCompatActivity

private static Intent serviceIntent;

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

    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};

    if(!hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }


    Button button = findViewById(R.id.button);
    button.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View view) {
                    serviceIntent = new Intent(MainActivity.this, ServiceS.class);
                    MainActivity.this.startService(serviceIntent);
                }
            });

}

public static boolean hasPermissions(Context context, String... permissions) {
    if (permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

ServiceS extends Service

public ServiceS() {
    super();
}


public static void locationArrived(Context context, Location location) {
    Log.d("hmm: ", location.toString());
}

@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
    initActivityTransition();
    super.onCreate();
}

@Override
public void onDestroy() {
    Log.d("hmm: ", "Updates stopped!");
    Task task = ActivityRecognition.getClient(this)
            .removeActivityTransitionUpdates(activityPendingIntent);
    super.onDestroy();
}

// ACTIVITY TRANSITION BLOCK START

private static List<ActivityTransition> transitions = new ArrayList<>();
private static PendingIntent activityPendingIntent;
private static ActivityTransitionRequest transitionRequest;

void initActivityTransition(){
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.IN_VEHICLE)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
                    .build());
    transitions.add(
            new ActivityTransition.Builder()
                    .setActivityType(DetectedActivity.IN_VEHICLE)
                    .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.WALKING)
                    .setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
                    .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());


    Intent activityIntentService = new Intent(this, TransitionReceiver.class);
    activityPendingIntent = PendingIntent.getBroadcast(this, 1, activityIntentService, PendingIntent.FLAG_UPDATE_CURRENT);

    Log.d("hmm: ","DriveBuddyService - initActivityTransition");
}

static void transitionArrived(final ActivityTransitionEvent event, final Context context){
    Log.d("hmm: ", event.toString());

    Toast.makeText(context, event.getActivityType() + "-" + event.getTransitionType(), Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {
    if(transitionRequest==null) {
        transitionRequest = new ActivityTransitionRequest(transitions);
    }
    Task task = ActivityRecognition.getClient(this)
            .requestActivityTransitionUpdates(transitionRequest, activityPendingIntent);
    super.onStart(intent, startId);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

TransitionReceiver extends BroadcastReceiver.

public void onReceive(final Context context, Intent intent) {
    Log.d("hmm: ","DriveBuddyTransitionReceiver - Enter");
    if (ActivityTransitionResult.hasResult(intent)) {
        ActivityTransitionResult result = ActivityTransitionResult.extractResult(intent);
        for (ActivityTransitionEvent event : result.getTransitionEvents()) {
            ServiceS.transitionArrived(event, context);
        }
    }
}
like image 770
ucanyiit Avatar asked May 09 '18 16:05

ucanyiit


People also ask

What is Activity Recognition API?

The Activity Recognition API automatically detects activities by periodically reading short bursts of sensor data and processing them using machine learning models.

What is Android activity Recognition?

The Activity Recognition Transition API can be used to detect changes in the user's activity. Your app subscribes to a transition in activities of interest and the API notifies your app only when needed. This page shows how to use the Activity Recognition Transition API, also called the Transition API for short.


1 Answers

I observed the same thing too. It seems that the last activity is kept in cache or something like that. I saw this happening on Android 7.1 and Android 8.1.

To counter this I check the elapsed time of when the transition happened. I ignore the callback if the transition happened over 30 seconds ago.

This is what my code looks like:

@Override
public void onReceive(Context context, Intent i) {

    if (ActivityTransitionResult.hasResult(i)) {
        ActivityTransitionResult result = ActivityTransitionResult.extractResult(i);

        for (ActivityTransitionEvent event : result.getTransitionEvents()) {
            //continue only if the activity happened in the last 30 seconds
            //for some reason callbacks are received for old activities when the receiver is registered
            if(((SystemClock.elapsedRealtime()-(event.getElapsedRealTimeNanos()/1000000))/1000) <= 30) {
                //activity transition is legit. Do stuff here..
            }
        }
    }
}

I've typically seen callbacks arrive 0-10 seconds of them occurring.

like image 170
MRK Avatar answered Oct 27 '22 01:10

MRK