Activity识别API不能连续工作

Activity Recognition API does not work continuously

我正在 2 个应用程序中测试 Activity 识别 API:Google 示例代码以及此代码在我的应用程序中的实现。

问题是这两个应用程序一直都能很好地获得 activity 识别,但几个小时后,intent 服务停止,我没有从它们两个获得任何 activity 识别。

知道为什么即使我已经用意图服务测试了地理围栏 api 并且它一直在不停地工作,但为什么意图服务会停止吗?

你的 IntentService 可能 "stop",我会说 "fall asleep" 因为:

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.

基本上API >= 20的大多数设备都应该是这样。

请寻找更多here

很遗憾,你只能请求activity更新,无法强制ActivityRecognitionApi持续给电流activity,即使是[=26] =] 对于一些神秘的 "extended period of time"。

在我看来,如果ActivityRecognitionApi的这个特性是可配置的,那就方便多了。

即使是在Manifest文件中注册broadcast receiver,也需要在Oreo+中动态注册,否则无法正常工作。在 activity 或 Service.It 中的 startCommand 中尝试 this.Add 此代码适用于 me.I 也在 Android 10 上测试过此代码..有效 perfectly.You 不需要在Manifest文件中注册broadcast receiver

@Override
    protected void onStart() {
        super.onStart();
        IntentFilter intentFilter=new IntentFilter(Constants.BROADCAST_DETECTED_ACTIVITY);
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(broadcastReceiver,intentFilter);

    }