延迟第一次修复活动识别API

时间:2015-09-16 07:32:02

标签: android performance activity-recognition

在我的Android应用中,我使用的是ActivityRecognition API,效果非常好。不幸的是,AR需要一些时间来获得第一个修复,我猜它等待一些重要动作以获得第一次修复或长时间以确定活动是否“仍然”,无论如何我将1000 ms设置为 detectionIntervalMillis 。听起来不错,但情况并非总是如此 - 有时即使手机仍然只是在重要动作之后仍然是AR更新活动并且仍未检测到。

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 1000, mPendingIntent);

当然我可以简单地同意默认是“仍然”,但我不想完全依赖AR API并且不想丢失一些位置修复(AR API在某些设备上无法正常工作,如果它们没有更新的Google Play服务),因此我设置了默认类似“no_activity_detected”的内容。

换句话说,我想将活动改为“静止”,以防AR API检测到它。

我的代码与developers.google上的代码和stackoverflow上的示例几乎相同,所以这应该不是问题。

MainActivity.java

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
@Override
    public void onConnected(Bundle bundle) {
        MyLog.d(TAG, "on Connect Google API");
        Intent intent = new Intent(this, ActivityRecognitionIntentService.class); // my custom ARS class
        mPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 1000, mPendingIntent);
    }

ActivityRecognitionIntentService.java:

@Override
    protected void onHandleIntent(Intent intent) {
/// Getting probable activity and sending a Broadcast with the recognized activity
}

作为解决方案,我看到了下一个选项:

1)编写另一个静态识别服务,该服务在AR API首次修复之前有效,默认情况下“no_activity_detected”已更改。

2)正如我在requestActivityUpdates中写的那样,以某种方式强制AR API在1000毫秒后给我一个更新的信息甚至不自信。它将确保AR API有效,我可以依赖它。

有没有更好的方法来解决我的问题?

0 个答案:

没有答案
相关问题