Android使用JobScheduler定期推送本地通知实例代码
程序员文章站
2022-06-04 21:41:33
android5.0之后提供了jobservice和jobscheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务。使用jobsch...
android5.0之后提供了jobservice和jobscheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务。使用jobscheduler,我们可以在用户一段时间没有使用我们的app的情况下,推送本地通知来提高app的用户留存率。废话不多说,上代码:
先在app的mainactivity启动时用jobscheduler来schedule一个job。注意在oncreate中我们把用户启动app的时间记录在了shared preference里面:
@override protected 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) //任何有网络的状态 .setpersisted(true) //系统重启后保留job .setperiodic(1000 * 60 * 60 * 24) //这里的单位是毫秒,1000 * 60 * 60 * 24代表一天(24小时) .build(); jobscheduler.schedule(jobinfo); } catch (exception ex) { log.e("schedulenotifications failure"); } } }
然后是推送通知的notificationservice,这里sharedpreferences是用的dagger2依赖注入,不用dagger的可以直接用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; //检查距离用户上一次启动app是否过了一定时间 if(intervalsincelastlaunch > 1000 * 60 * 60 * 24) { notificationcompat.builder mbuilder = new notificationcompat.builder(notificationservice.this) .setautocancel(true) .setsmallicon(r.mipmap.ic_launcher) .setcontenttitle("我的app") .setcontenttext("又有新的内容上线了,快来我们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; } }
最后需要在manifest中注册我们的service和申请相关的权限:
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.receive_boot_completed" /> <service android:name=".notificationservice" android:permission="android.permission.bind_job_service" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。