首页 > 系统 > Android > 正文

Android使用JobScheduler定期推送本地通知实例代码

2019-12-12 02:34:00
字体:
来源:转载
供稿:网友

Android5.0之后提供了JobService和JobScheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务。使用JobScheduler,我们可以在用户一段时间没有使用我们的app的情况下,推送本地通知来提高app的用户留存率。废话不多说,上代码:

先在app的MainActivity启动时用JobScheduler来schedule一个job。注意在onCreate中我们把用户启动app的时间记录在了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) //任何有网络的状态            .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" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表