如何为api 21修复context.getSystemService(class)?

Moh*_*ara 6 java android android-jobscheduler

我正在关注视频JobScheduler:

https://www.youtube.com/watch?v=XFN3MrnNhZA

但是lint给了我那context.getSystemService(class)api 23.因为我错过了一些东西或者是否有机器人用api改变了什么?

注意:大多数人想知道是否JobScheduler介绍了api 21如何在没有它的情况下工作getSystemService(Class)

谢谢.

Moh*_*ara 10

感谢Reghunandan我使用以下方法找到了更好的解决方案:

github.com/firebase/firebase-jobdispatcher-android

但如果有任何身体在这里遇到同样的问题是错误:

getSystemService(Class) API 23
Run Code Online (Sandbox Code Playgroud)

应该:

getSystemService(String) API 1

getSystemService(context.JOB_SCHEDULER_SERVICE) API 21
Run Code Online (Sandbox Code Playgroud)

  • 另外,我必须将其转换为`jobScheduler`对象,如下所示.`JobScheduler jobScheduler =(JobScheduler)getApplicationContext().getSystemService(JOB_SCHEDULER_SERVICE);` (4认同)
  • 在 Kotlin 中,[cast](https://kotlinlang.org/docs/typecasts.html#unsafe-cast-operator) 如下: `getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager` (2认同)

小智 6

来到这里,NotificationManager 也遇到了类似的问题。我最终做的是

 val notificationManager =
                ContextCompat.getSystemService(
                    requireContext(),
                    NotificationManager::class.java
                ) as NotificationManager
Run Code Online (Sandbox Code Playgroud)