【发布时间】:2014-04-17 08:56:32
【问题描述】:
我的 Android 应用需要检测当前的 Activity(跑步、步行等)。
我按照 Android 开发页面上的说明进行操作: http://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionClient.html
// Connect to the ActivityRecognitionService
ActivityRecognitionClient mActivityRecognitionClient =
new ActivityRecognitionClient(this, this, this);
mActivityRecognitionClient.connect();
// Called when a connection to the ActivityRecognitionService has been established.
public void onConnected(Bundle connectionHint) {
Intent intent = new Intent(this, MyIntentService.class);
PendingIntent callbackIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mActivityRecognitionClient.requestActivityUpdates(30000, callbackIntent);
}
在我的 Intent-Service 中:
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Put your application specific logic here (i.e. result.getMostProbableActivity())
}
}
这是我的问题:我想自己启动活动识别。 Android Dev Page 中的代码似乎应该这样做。但是,如果我不将该服务添加到 AndroidManifest,它不会做任何事情。 onConnected() 方法被调用,但不是 onHandleIntent()。
所以我想我可能只需要将它添加到清单中。现在我确实收到了活动,但我一直收到它们。这也不是我想要的。我做错了什么?
提前致谢, 斯特夫
【问题讨论】:
标签: android google-play-services activity-recognition android-intentservice