【发布时间】:2019-11-05 16:33:37
【问题描述】:
我正在创建一个应用程序,即使应用程序从进程中或在后台被终止,它也会向用户发送通知。实际上,在 SDK >= 26 的 Android 版本中,我使用的是前台服务。 为了显示通知,我有一个服务 (DatasNotification),它启动一个 asyncTask (HttpRequest) 以发出 http 请求,并且在条件结束时的 asynctask 启动一个显示通知的新服务。 当我的应用程序进入后台时,我有一个系统通知说:我的应用程序正在耗尽电池。 在我的分析过程中,我了解到这个系统通知是由 DatasNotification 服务生成的。
这是我的 DatasNotification 代码:
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1, new Notification());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new HttpRequest(getApplicationContext(), 0).execute();
return START_STICKY;
}
sdk >= 26 运行后台服务有什么解决办法吗?
【问题讨论】:
标签: android service notifications