GoogleApiClient和活动检测问题:在相等的时间内未检测到活动

时间:2019-02-24 18:42:42

标签: java android android-activity google-api-client android-activityrecord

我想检测GoogleApiClient的活动。但我无法让它检测到相等的周期。我在onCreate方法中使用此代码来检测活动。 例如,第一个对数出现在7':7“,第二个对数出现在8':1”,第三个对数出现在12':14“。

mApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mApiClient.connect();

onConnected方法:

@Override
public void onConnected( Bundle bundle) {

    Intent intent = new Intent( this, ActivityRecognizedService.class );
    PendingIntent pendingIntent = PendingIntent.getService( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
    ActivityRecognitionClient activityRecognitionClient = ActivityRecognition.getClient(this);
    Task task = activityRecognitionClient.requestActivityUpdates(0, pendingIntent);

}

最终的ActivityRecognizedService类:

public class ActivityRecognizedService extends IntentService {
public ActivityRecognizedService() {
    super("ActivityRecognizedService");
}

public ActivityRecognizedService(String name) {
    super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
    if(ActivityRecognitionResult.hasResult(intent)) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        handleDetectedActivities( result.getProbableActivities() );
    }
}

private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
    for( DetectedActivity activity : probableActivities ) {
        Log.e( "ActivityRecogition", "detected activity in service "+ activity.toString());
        switch( activity.getType() ) {
            case DetectedActivity.IN_VEHICLE: {
                Log.e( "ActivityRecogition", "In Vehicle: " + activity.getConfidence() );
                break;
            }
            case ...
        }
    }
}
  1. 如何将检测限制在特定时间段内?
  2. 如何在特定时段强制进行活动检测?

通过这段代码,情况变得更好了。

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

但是我想要另一种方式...

0 个答案:

没有答案