Activity Recognition using new GoogleApiClient not firing Activity Updates

久未见 提交于 2019-12-24 12:22:31

问题


I am trying to set up ActivityRecognition in my app using the new GoogleApiClient. I couldn't find any documentation anywhere (Official android docs still refer to the now deprecated ActivityRecognitionClient).

The code that I managed to write is not triggering Activity Updates.

1) Inside my MainActivity's onCreate :

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

mGoogleApiClient.connect();

2) Inside 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);

My ActivityRecognitionIntentService never receives an update.

What am I doing wrong? Or is there any sort of documentation on ActivityRecognition through GoogleApiClient anywhere?


回答1:


Solved it. After connecting to GoogleApiClient I just had to do this to start receiving Activity Updates.

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

No need to create your own ActivityRecognitionApi object.



来源:https://stackoverflow.com/questions/27634443/activity-recognition-using-new-googleapiclient-not-firing-activity-updates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!