In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use JobScheduler to push local notice regularly in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this Android article on how to use JobScheduler to push local notice periodically. Let's take a look.
Following Android5.0, JobService and JobScheduler are provided to perform a task at a later point in time or when a particular condition is met. With JobScheduler, we can push local notifications to improve the user retention rate of app if the user has not used our app for a period of time. Don't talk too much nonsense, put on the code:
First use JobScheduler to schedule a job when app's MainActivity starts. Note that in onCreate, we record the time when the user starts app in shared preference:
@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); sharedPreferences.edit () .putLong (Constants.SP_PARAM_LAST_LAUNCH, System.currentTimeMillis ()) .apply (); scheduleNotifications ();} private void scheduleNotifications () {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {try {JobScheduler jobScheduler = (JobScheduler) getSystemService (JOB_SCHEDULER_SERVICE) JobInfo jobInfo = new JobInfo.Builder (1, new ComponentName (getPackageName (), NotificationService.class.getName () .setRequiresCharging (false) .setRequiredNetworkType (JobInfo.NETWORK_TYPE_ANY) / / any state with a network. SetPersisted (true) / / retain job .setPeriodic (1000 * 60 * 60 * 24) / / the unit here is milliseconds. 1000 * 60 * 60 * 24 represents a day (24 hours) .build () JobScheduler.schedule (jobInfo);} catch (Exception ex) {Log.e ("scheduleNotifications failure");}
Then there is the NotificationService of push notification, where SharedPreferences uses dagger2 dependency injection, and those that do not need dagger can be obtained directly with PreferenceManager.getDefaultSharedPreferences:
@ RequiresApi (api = Build.VERSION_CODES.LOLLIPOP) public class NotificationService extends JobService {@ DefaultSharedPref @ Inject SharedPreferences sharedPreferences; @ Override public boolean onStartJob (JobParameters params) {try {long lastLaunchTime = sharedPreferences.getLong (Constants.SP_PARAM_LAST_LAUNCH,-1); if (lastLaunchTime > 0) {long intervalSinceLastLaunch = System.currentTimeMillis ()-lastLaunchTime / / check whether if (intervalSinceLastLaunch > 1000 * 60 * 60 * 24) {NotificationCompat.Builder mBuilder = new NotificationCompat.Builder (NotificationService.this) .setAutoCancel (true) .set SmallIcon (R.mipmap.ic_launcher) has elapsed since the user last started app. .setContentTitle ("my app") .setContentText ("there is something new online. Come and have a look at our app! Intent resultIntent = new Intent (NotificationService.this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create (NotificationService.this); stackBuilder.addParentStack (MainActivity.class); stackBuilder.addNextIntent (resultIntent) PendingIntent resultPendingIntent = stackBuilder.getPendingIntent (0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent (resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE); mNotificationManager.notify (1, mBuilder.build ()) }} catch (Exception ex) {Log.e ("Exception in NotificationService onStartJob");} return false;} @ Override public boolean onStopJob (JobParameters params) {Log.d ("NotificationService onStopJob"); return true;}}
Finally, we need to register our service in Manifest and apply for the relevant permissions:
This is the end of the article on "how to use JobScheduler to push local notifications on a regular basis in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use JobScheduler to push local notifications regularly in Android". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.