我正在尝试使用新的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的任何文档?

最佳答案

解决了。连接到GoogleApiClient之后,我只需要开始接收活动更新即可。

ActivityRecognition.ActivityRecognitionApi.
  requestActivityUpdates(mGoogleApiClient, 0, mActivityRecognitionPendingIntent);


无需创建自己的ActivityRecognitionApi对象。

关于android - 使用新的GoogleApiClient进行 Activity 识别不会触发 Activity 更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27634443/

10-10 20:23