Android -Starting Service at Boot Time

后端 未结 9 1733
一个人的身影
一个人的身影 2020-11-22 16:12

I need to start a service at boot time. I searched a lot. They are talking about Broadcastreceiver. As I am new to android development, I didn\'t get a clear picture about s

9条回答
  •  遥遥无期
    2020-11-22 16:46

    Pls check JobScheduler for apis above 26

    WakeLock was the best option for this but it is deprecated in api level 26 Pls check this link if you consider api levels above 26
    https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html#startWakefulService(android.content.Context,%20android.content.Intent)

    It says

    As of Android O, background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use android.app.job.JobScheduler to schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).

    so as it says cosider JobScheduler
    https://developer.android.com/reference/android/app/job/JobScheduler

    if it is to do something than to start and to keep it you can receive the broadcast ACTION_BOOT_COMPLETED

    If it isn't about foreground pls check if an Accessibility service could do

    another option is to start an activity from broadcast receiver and finish it after starting the service within onCreate() , since newer android versions doesnot allows starting services from receivers

提交回复
热议问题