ActivityRecognitionService(Google API)无法正常运行

时间:2016-06-14 18:16:17

标签: android api google-api

我使用本教程开发了我的应用程序: 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;
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

根据requestActivityUpdates() documentation

  

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

     

如果活动检测服务需要更多样本来进行更准确的预测,活动可能会在请求的detectIntervalMillis后几秒钟到达。

  

从API 21开始,如果设备处于省电模式且屏幕关闭,则可能会比detectIntervalMillis参数更少地接收活动。

因此,您不应该期望每3秒钟完成一次呼叫,但假设您获得的每次呼叫都是状态变化的开始 - 如果您还没有收到任何回叫,那是因为没有任何变化。

另请注意,停止接收更新的正确通话为removeActivityUpdates(),并传入与PendingIntent相同的requestActivityUpdates()