电话进入睡眠状态后,活动识别功能无效

时间:2015-10-27 08:43:16

标签: android-sqlite google-play-services wakelock android-wake-lock activity-recognition

免责声明:这不是Activity Recognition stops receiving updates when phone goes to standby(screen off state)的重复,因为我已经使用了这种方法而且没有帮助。

问题:

似乎活动识别服务停止工作,并且在设备进入睡眠状态时不会发送任何更新。当设备没有睡眠时,它可以正常工作(已识别的活动存储到sqlite)但是当我按下HTC M8上的电源按钮时,此时不会存储任何内容,直到我唤醒设备但再次按下电源按钮。

我正在进行活动识别的一些细节:

请求活动识别更新看起来像这样(取自official google samples,但我将IntentService更改为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);
    }

}

接收活动识别更新看起来像这样(标准WakefulBroadcastReceiver + IntentService合作)

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);
    }
}

我正在使用compile 'com.google.android.gms:play-services-location:8.1.0'

EDIT1:

在阅读了用户bjiang的提示后,这个答案https://stackoverflow.com/a/32965481/2401535似乎在HTC设备上存在一些问题,因为它不像活动识别那样工作api docs说:

  

为了节省电量,当设备长时间“静止”时,活动报告可能会停止。一旦设备再次移动,它将恢复。这仅在支持Sensor.TYPE_SIGNIFICANT_MOTION硬件的设备上发生。

HTC M8确实支持Sensor.TYPE_SIGNIFICANT_MOTION,因此活动识别应在device moves again之后重新开始。但事实并非如此。用电源按钮唤醒设备后,它会启动活动识别。

0 个答案:

没有答案