android - 还必须在前台启动 Android 子服务吗?
问题描述
在我的 Android 应用程序中,我记录了很多东西,比如 GPS、气压、蓝牙传感器。有一个“超级”服务在前台启动:
Intent intent = new Intent(context, DataCollectorService.class);
ContextCompat.startForegroundService(context, intent);
该服务本身必须启动许多子服务,如 GPS 记录、蓝牙传感器记录等。该服务会创建一个通知:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// . . .
Notification notification = new NotificationCompat.Builder(this, channelId)
// . . .
.build();
startForeground(NOTIFICATION_ID, notification);
mHandler.postDelayed(this::startChildServices, 100);
return START_STICKY;
}
private void startChildServices() {
// . . .
if (gpsEnabled) {
Intent intent = new Intent(this, GpsTrackingService.class);
bindService(intent, mGpsServiceConnection, Context.BIND_AUTO_CREATE);
startService(intent);
}
if (isPressureEnabled) {
Intent intent = new Intent(this, PressureService.class);
bindService(intent, pressureConnection, Context.BIND_AUTO_CREATE);
startService(intent);
}
// . . .
}
一切正常,但极少数用户会遇到这样的崩溃:
android.app.RemoteServiceException: Context.startForegroundService() 然后没有调用 Service.startForeground()
实际上,如您所见,我在前台启动了“超级”服务。
我是否也必须在前台启动子服务?
显然,我不想在用户状态栏中有 3-7 个通知图标。
解决方案
推荐阅读
- r - 返回值最接近 2 个表之间的日期
- python - 从零开始实现条件逻辑回归
- amazon-web-services - Flask-WTF“CSRF 令牌不匹配”与 AWS ECS 和 ELB
- path - 有没有办法在 macOS 上重置 PATH 变量?
- python - matplotlib 制作线条而不是正常的散点图
- javascript - 地理位置 API
- amazon-web-services - 在 AWS CI-CD 管道中找不到 NVM
- ocaml - 如何将值提升到 monad
- material-ui - 如何在不知道是否启用深色主题的情况下获取当前的原色和二次色?
- javascript - audio.play() 给我“未捕获(承诺)DOMException 错误”