我的Android ActivityRecognition实现太慢了

时间:2016-08-20 10:02:25

标签: android activity-recognition

我使用Google ActivityRecognitionApi跟踪我是否正在行走。 但似乎太慢了。问题是什么? 它与互联网连接无关。

[Walking.java]

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_walking);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
    Intent intent = new Intent(this, ActivityRecognitionService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 1000, pendingIntent);
}
@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(Walking.this, "Hello1", Toast.LENGTH_SHORT).show();
    Log.e("Connection Failed : ", "Connection Failed");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(Walking.this, "Hello2", Toast.LENGTH_SHORT).show();
    Log.e("Connection Failed : ", "Connection Failed");
}

[ActivityRecognitionService.java]

import java.util.List;
public class ActivityRecognitionService extends IntentService {
    public static final String ACTION_IntentService = "com.example.android.skywalker.RESPONSE";
    public static final String ACTIVITY_RESULT = "RESULT";
    public Intent localIntent;
    public ActivityRecognitionService() {
        super("ActivityRecognitionService");
    }
    public ActivityRecognitionService(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) {
        int Flag = 0;

        for(DetectedActivity activity : probableActivities) {
            if(activity.getType() == DetectedActivity.ON_FOOT) {
                Flag = 1;
                if (activity.getConfidence() >= 50) Log.e("YES", "YES");
                break;
            }
        }

        //Intent intent = new Intent(ActivityRecognitionService.this, Walking.class);
        if (Flag == 0) {
            //localIntent = new Intent(Walking.BROADCAST_ACTION).putExtra(Walking.RESPONSE_STATUS, "YES");
            Log.e("NO", "NO");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为您的代码没有任何问题,它看起来与我的完全相同。 正常的检测率是3秒 - >如果“detectionIntervalMillis”值= 0,则为6秒。 但是当你在两个不同的活动之间切换时(例如:从步行 - >在车里......),它需要更多,可能是20到40秒。