我在2个应用中测试了Activity Recognition API:Google示例代码以及在我的应用中实现此代码。
问题是两个应用程序一直使活动识别正常,但几个小时后意图服务停止,我没有得到它们的任何活动识别。
即使我已经使用意向服务测试了geofencing api并且它在没有停止的情况下永远工作,但是为什么意图服务停止了?
答案 0 :(得分:4)
Your IntentService might "stop", I would say "fall asleep" because of:
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.
Basically it should be the case for most of the devices with API >= 20.
Please find more here.
Unfortunately, you can only request activity updates and it is impossible to force ActivityRecognitionApi to continuously give current activity even if it is "still" for some mysterious "extended period of time".
In my opinion, it would much more convenient if this feature of ActivityRecognitionApi was configurable.
答案 1 :(得分:0)
即使在Manifest文件中注册了广播接收器,您也需要在Oreo +中动态注册,否则它将不起作用。尝试一下。在主要活动中或在Service的startCommand中添加此代码。它对我有用。我也已在Android 10上测试了此代码。.运行完美。您无需在清单文件中注册广播接收器。
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter=new IntentFilter(Constants.BROADCAST_DETECTED_ACTIVITY);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(broadcastReceiver,intentFilter);
}