Android uses jobscheduler to regularly push local notification instance code

After Android 5.0, jobservice and jobscheduler are provided to execute a task at a later point in time or when a specific condition is met. Using jobscheduler, we can push local notifications to improve the user retention rate of the app when users haven't used our app for a period of time. No more nonsense, code:

First, use the jobscheduler to schedule a job when the app's mainactivity starts. Note that in oncreate, we record the time when the user starts the app in the shared preference:

Then comes the notificationservice for pushing notifications. Here SharedPreferences are dagger2 dependency injection. If there is no dagger, you can directly use preferencemanager.getdefaultsharedpreferences:

Finally, we need to register our service in manifest and apply for relevant permissions:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>