The interval of results from Google's activity

2019-07-04 10:00发布

问题:

I was working on google's activity recognition and finally I got results from this service.

However, The function requestActivityUpdates(long detectionIntervalMillis, PendingIntent callbackIntent) doesn't seem to work correctly. The detection interval is not regular and decreases to 30~50 seconds when my current activity is changing. It doesn't work like the live demo on google I/O 2013 (Google I/O 2013 Location API, from 27:47 to 28:45). Does anyone have same issue on it?

回答1:

Well it's a bit confusing but the variable long detectionIntervalMillis does not fix the interval - it sets the maximum interval between two updates.

As it's said in the official documentation:

Activities may be received more frequently than the detectionIntervalMillis parameter if another application has also requested activity updates at a faster rate. It may also receive updates faster when the activity detection service receives a signal that the current activity may change, such as if the device has been still for a long period of time and is then unplugged from a phone charger.

Documentation



回答2:

Instead of adding the build() part on onCreate directly try adding the method below and call it on onCreate(). And try to trace the code from google sample I got the code stripped down from that sample.

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}

and add buildGoogleApiClient() to your onCreate method ;) this works great for me!