问题
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://stackoverflow.com/a/32965481/2401535 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.
来源:https://stackoverflow.com/questions/33363274/activity-recognition-does-not-work-after-phone-goes-asleep