我想检测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 ...
}
}
}
通过这段代码,情况变得更好了。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
但是我想要另一种方式...