我正在创建一个应用程序,即使该应用程序在进程中或在后台被杀死,该应用程序也会向用户发送通知。实际上,在SDK> = 26的Android版本中,我正在使用前台服务。 为了显示通知,我有一个服务(Datasnotification),该服务启动用于发出http请求的asyncTask(HttpRequest),条件结束时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中有运行后台服务的解决方案吗?