【发布时间】:2018-10-06 09:13:49
【问题描述】:
在我的应用程序中,我正在启动一个前台服务并显示一个进度对话框来更新用户的进度。在某些手机上,启动前台服务会导致 ui 延迟几秒钟。如果我禁用以下代码,则 ui 响应正常。
启动服务的代码是applicationContext.startService(FirmwareUpdateService.createUpdateActionIntent(applicationContext, progress));
如果我将上面的代码注释掉,那么什么都不会冻结。
如果我对服务内部的逻辑进行注释(仅显示前台通知),则没有任何区别。
private void createUpdateNotification(int progress) {
return new Notification.Builder(this) .setContentTitle(getString(R.string.updating)) .setProgress(MAX_PROGRESS, progress, false) .setSmallIcon(R.drawable.push_notif_icon) .build();
}
Notification notification = createUpdateNotification(progress);
startForeground(NOTIFICATION_ID, notification);
在第一次调用启动服务后,连续调用不会冻结 UI。任何见解都值得赞赏!
【问题讨论】:
-
在第一次调用启动
Service时,会调用Service的构造函数,并在Service中调用onCreate()。您是否有任何可能需要大量时间的代码? -
@DavidWasser 我们唯一的代码在
onStartCommand(),它是显示如上所示的通知。 -
把代码贴在
createUpdateNotification() -
@DavidWasser
return new Notification.Builder(this) .setContentTitle(getString(R.string.updating)) .setProgress(MAX_PROGRESS, progress, false) .setSmallIcon(R.drawable.push_notif_icon) .build(); -
以后不要把代码放在注释里。您可以编辑您的原始问题并在那里添加代码。它更容易阅读。
标签: android android-service ui-thread foreground-service