第一个转换始终相同 - 活动识别API - 活动转换

时间:2018-05-09 16:21:17

标签: java android activity-recognition

我使用活动识别api和活动转换。当我第一次运行应用程序时,第一次转换始终是。在这个应用程序中WALKING-ENTER。当我在IN_VEHICLE-ENTER中仅使用IN_VEHICLE-EXITtransitions时,它是IN_VEHICLE-ENTER。我想忽略了第一次转换,但是我测试过的设备并没有出现这样的问题。有这些问题的设备是Android 8.1,设备没有问题是6.0。

MainActivity扩展了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扩展服务

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扩展了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);
        }
    }
}

1 个答案:

答案 0 :(得分:8)

我也观察到同样的事情。似乎最后一个活动保存在缓存中或类似的东西。我在Android 7.1和Android 8.1上看到了这种情况。

为了解决这个问题,我会检查过渡发生的经过时间。如果转换发生在5秒前,我会忽略回调。

这就是我的代码:

@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 5 seconds
            //for some reason callbacks are received for old activities when the receiver is registered
            if(((SystemClock.elapsedRealtime()-(event.getElapsedRealTimeNanos()/1000000))/1000) <= 5) {
                //activity transition is legit. Do stuff here..
            }
        }
    }
}

我经常看到回调在0-1.5秒内到达。