我在Android O操作系统上使用服务类。
我计划在后台使用服务。
Android文档指出
如果你的应用程序的API级别为26或更高,系统会对使用或创建后台服务施加限制,除非应用程序本身在前台。如果应用程序需要创建前台服务,应用程序应该调用startForegroundService()。
如果使用startForegroundService(),服务抛出以下错误。
Context.startForegroundService() did not then call
Service.startForeground()
这有什么问题?
https://developer.android.com/reference/android/content/Context.html startForegroundService (android.content.Intent)
类似于startService(Intent),但是有一个隐含的承诺
Service将调用start前台(int, android.app.Notification)一次
它开始运行。该服务被赋予了相当的时间
到ANR区间做这个,否则系统会
自动停止服务并声明应用程序ANR。
与普通的startService(Intent)不同,这个方法可以在
任何时候,不管托管服务的应用程序是否在
前景的国家。
一定要打电话给服务处。在onCreate()上的start前台(int, android.app.Notification),所以你确保它将被调用..如果你有任何条件,可能会阻止你这样做,那么你最好使用正常的Context.startService(意图)并调用服务。start前台(int, android.app.Notification)你自己。
似乎Context.startForegroundService()添加了一个看门狗来确保你调用了服务。start前台(int, android.app.Notification)在它被销毁之前…