我正在尝试使用新的GoogleApiClient在我的应用中设置ActivityRecognition。我在任何地方都找不到任何文档(官方的android文档仍然引用现在已弃用的ActivityRecognitionClient)。
我设法写的代码没有触发活动更新。
1)在我的MainActivity的onCreate中:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
2)在onConnected内部:
Intent i = new Intent(this, ActivityRecognitionIntentService.class);
PendingIntent mActivityRecognitionPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Log.e("Aditya", "Connected to ActRec");
ActivityRecognitionApi actRecAPI = new ActivityRecognitionApi()
{
public PendingResult<Status> requestActivityUpdates(GoogleApiClient googleApiClient, long l, PendingIntent pendingIntent)
{
Log.e("Aditya", "inside requestActivityUpdates()");
return null;
}
public PendingResult<Status> removeActivityUpdates(GoogleApiClient googleApiClient, PendingIntent pendingIntent)
{
return null;
}
};
actRecAPI.requestActivityUpdates(mGoogleApiClient, 0, mActivityRecognitionPendingIntent);
我的ActivityRecognitionIntentService永远不会收到更新。
我做错了什么?或者在任何地方通过GoogleApiClient提供有关ActivityRecognition的任何文档吗?
答案 0 :(得分:3)
解决了它。连接到GoogleApiClient后,我必须这样做才能开始接收活动更新。
ActivityRecognition.ActivityRecognitionApi.
requestActivityUpdates(mGoogleApiClient, 0, mActivityRecognitionPendingIntent);
无需创建自己的ActivityRecognitionApi对象。