【问题标题】:ActivityRecognitionService (Google API) is not working properlyActivityRecognitionService (Google API) 无法正常工作
【发布时间】:2016-06-14 18:16:17
【问题描述】:

我使用本教程开发了我的应用程序: http://code.tutsplus.com/tutorials/how-to-recognize-user-activity-with-activity-recognition--cms-25851

即使我每 3 秒请求一次更新(或更改,5 秒、10 秒等);生成值的时间非常不一致。有时它会在 10 分钟内生成 4 个值!为什么 API 如此不一致?此外,我无法断开 API,即使在调用 API.disconnect() 之后,我仍然会不断在 logcat 中获取值,这会导致手机发热并过度消耗电池。

这是完整的项目:https://github.com/AseedUsmani/MotionAnalyser2

基本代码:

1) 活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analysing);
    mApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
   mStartButton = (Button) findViewById(R.id.startButton);
   mFinishButton = (Button) findViewById(R.id.finishButton);
   mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //resetting counter 
             for (int j = 0; j < 8; j++) {
                    mCount[j] = 0;
                }
                mServiceCount = 0;
                mApiClient.connect();
                mStartButton.setVisibility(View.INVISIBLE);
                mFinishButton.setVisibility(View.VISIBLE);
             }
        }
   mFinishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mApiClient.disconnect();
}
}
}
@Override
    public void onConnected(@Nullable Bundle bundle) {
        Intent intent = new Intent(this, ActivityRecognizedService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                 ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, 3000, pendingIntent);
    }


@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(AnalysingActivity.this, "Connection to Google Services suspended!", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Toast.makeText(AnalysingActivity.this, "Connection to Google Services failed!", Toast.LENGTH_LONG).show();
}
}

2) 服务:

public class ActivityRecognizedService extends IntentService {

AnalysingActivity mObject = new AnalysingActivity();
int confidence;


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) {
    confidence = mObject.confidence;
    mObject.mServiceCount++;

    for (DetectedActivity activity : probableActivities) {
        switch (activity.getType()) {
            case DetectedActivity.IN_VEHICLE: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[0]++;
                }
                mObject.mActivity[0] = "In Vehicle: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[0]);
                Log.e("ActivityRecogition", "In Vehicle: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[0]));
                break;
            }
            case DetectedActivity.ON_BICYCLE: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[1]++;
                }
                mObject.mActivity[1] = "Cycling: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[1]);
                Log.e("ActivityRecogition", "Cycling: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[1]));
                break;
            }
            case DetectedActivity.ON_FOOT: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[2]++;
                }
                mObject.mActivity[2] = "On Foot: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[2]);
                Log.e("ActivityRecogition", "On foot: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[2]));
                break;
            }
            case DetectedActivity.RUNNING: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[3]++;
                }
                mObject.mActivity[3] = "Running: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[3]);
                Log.e("ActivityRecogition", "Running: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[3]));
                break;
            }
            case DetectedActivity.STILL: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[4]++;
                }
                mObject.mActivity[4] = "Still: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[4]);
                Log.e("ActivityRecogition", "Still: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[4]));
                break;
            }

            case DetectedActivity.WALKING: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[5]++;
                }
                mObject.mActivity[5] = "Walking: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[5]);
                Log.e("ActivityRecogition", "Walking: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[5]));
                break;
            }

            case DetectedActivity.TILTING: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[6]++;
                }
                mObject.mActivity[6] = "Tilting: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[6]);
                Log.e("ActivityRecogition", "Tilting: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[6]));
                break;
            }

            case DetectedActivity.UNKNOWN: {
                if (activity.getConfidence() >= confidence) {
                    mObject.mCount[7]++;
                }
                mObject.mActivity[7] = "Unknown: " + Integer.toString(activity.getConfidence()) + " " + Integer.toString(mObject.mCount[7]);
                Log.e("ActivityRecogition", "Unknown: " + activity.getConfidence() + " " + Integer.toString(mObject.mCount[7]));
                break;
            }
        }
    }
}
}

【问题讨论】:

    标签: android api google-api


    【解决方案1】:

    根据requestActivityUpdates() documentation:

    如果另一个应用程序也以更快的速度请求活动更新,则可能会比 detectionIntervalMillis 参数更频繁地接收活动。当活动检测服务接收到当前活动可能发生变化的信号时,它也可以更快地接收更新,例如如果设备长时间静止然后从手机充电器上拔下。

    如果活动检测服务需要更多样本才能做出更准确的预测,则活动可能会在请求的 detectionIntervalMillis 几秒后到达。

    和

    从 API 21 开始,如果设备处于省电模式且屏幕关闭,则接收活动的频率可能低于 detectionIntervalMillis 参数。

    因此,您不应期望每 3 秒调用一次,而是假设您收到的每个调用都是状态更改的开始 - 如果您没有收到任何回调,那是因为没有任何更改。

    另请注意,停止接收更新的正确调用是 removeActivityUpdates(),传递的 PendingIntent 与传递给 requestActivityUpdates() 的相同。

    【讨论】:

    • 状态变化开始到底是什么意思?喜欢从步行到开车?有没有办法让 API 更加一致?
    • 是的,在您收到回调之前,活动和信心不会改变。只有当它们发生变化时,您才会收到回调。
    • 但是,即使我完全改变了活动,例如从步行到开车或静止,我也没有接到电话。早些时候,即使我的手机保持静止,我也能更稳定地获得价值,我会以 100 的信心获得 Still。是否与 logcat 中的这条消息有关-“W/art: Suspending all threads ...”。是否与我正在使用的可运行对象/处理程序有关?
    • 也许可以尝试一些更简单的方法。您当前的代码过度设计,错误地使用了静态变量,postDelayed 延迟为 0,甚至可能根本不需要 Service(您的 Activity 中的 BroadcastReceiver 会简单得多)。 API 按文档说明工作。
    • 我想在屏幕上显示数据,并且还要计算特定活动被识别的次数。在不使用静态变量的情况下如何做到这一点?另外,我必须设置一个计时器,所以我必须使用多线程,我必须访问 GPS 位置并在需要时将数据保存在文件中,因此可能会过度设计。基本上我必须开发一个应用程序来检测用户何时停车,所以我尝试检测驾驶和步行。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多