Activity Recognition does not work after phone goes asleep
Asked Answered
S

0

4

Disclaimer: this is not duplicate of Activity Recognition stops receiving updates when phone goes to standby(screen off state) because I'm already using that approach and it does not help.

Problem:

It seems that Activity Recognition service stops working and is not sending any updates when device goes asleep. It works OK (recognized activities are stored to sqlite) when device is not asleep but as soon as I press power button on HTC M8, nothing is stored from this moment until I wake up the device but pressing power button again.

Some details how I'm doing activity recognition:

Requesting activity recognition updates looks like this (taken from official google samples but I changed IntentService to BroadcastReceiver):

public class DevActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    //...

    private PendingIntent getActivityDetectionPendingIntent() {
        Intent intent = new Intent(this, ActRecReceiver.class);
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    public void requestActivityUpdatesButtonHandler(View view) {
        if (!googleApiClient.isConnected()) {
            Toast.makeText(this, "not_connected", Toast.LENGTH_SHORT).show();
            return;
        }
        final Preferences pref = new Preferences(this);
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                googleApiClient,
                pref.getActRecInterval(0),
                getActivityDetectionPendingIntent()
        ).setResultCallback(this);
    }

}

Receiving activity recognition updates looks like this (standard WakefulBroadcastReceiver + IntentService cooperation)

public class ActRecReceiver extends WakefulBroadcastReceiver {

    private static final String TAG = "ActRecReceiver";
    private static final boolean LOG = BuildConfig.LOGGING;

    @Override
    public void onReceive(Context context, Intent intent) {

        if (LOG) Log.d(TAG, "onReceive");
        Intent serviceStarter = new Intent(context, DetectedActivitiesIntentService.class);
        serviceStarter.putExtra("ActivityRecognitionResult", ActivityRecognitionResult.extractResult(intent));
        startWakefulService(context, serviceStarter);
    }
}

public class DetectedActivitiesIntentService extends IntentService {

    protected static final String TAG = "DetectedActivitiesIS";

    public DetectedActivitiesIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ActivityRecognitionResult result = intent.getParcelableExtra("ActivityRecognitionResult");
        List<DetectedActivity> detectedActivities = result.getProbableActivities();
        HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
        for (DetectedActivity activity : detectedActivities) {
            detectedActivitiesMap.put(activity.getType(), activity.getConfidence());
        }

        storeEventToSqlite(detectedActivitiesMap);

        ActRecReceiver.completeWakefulIntent(intent);
    }
}

I'm using compile 'com.google.android.gms:play-services-location:8.1.0'

EDIT1:

After reading hints from user bjiang and this answer https://mcmap.net/q/1329468/-activity-recognition-api-does-not-work-continuously it seems that there is some problem on HTC devices because it does NOT work like Activity Recognition api docs says:

To conserve battery, activity reporting may stop when the device is 'STILL' for an extended period of time. It will resume once the device moves again. This only happens on devices that support the Sensor.TYPE_SIGNIFICANT_MOTION hardware.

HTC M8 does indeed support Sensor.TYPE_SIGNIFICANT_MOTION so Activity Recognition should start again after device moves again. But it does NOT. It does starts activity recognition after I wake up device with power button.

Strapper answered 27/10, 2015 at 8:43 Comment(5)
Please make sure to check this answer that others and me once helped. It will help you also for the still statues Activity Recognition.Happening
Thanks, but it does not help - in my case it does not work only on HTC M8. Not getting STILL is OK if device is indeed not moving, but in my case I do NOT receive absolutely ANY Activity Recognition updates if device actually is moving. I get those as soon as I woke up the device.Angulo
How about this answer. I pointed another answer, sorry. Also, please compare your code with this repo which was written by a googler.Happening
It could be due to a bug with the accelerometer when the screen is turned off. Test your app on a different model phone to see if the issue persists. If that is not possible, write a quick app that registers to the SensorManager and prints out the accelerometer data, then turn off you screen. If it stops, then you phone has that bug, and Google Play services will probably not work due to it. Check out this link to read more about it: #9982933Balough
Thanks, this may indeed be related, appreciated.Angulo

© 2022 - 2024 — McMap. All rights reserved.