栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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和申请相关的权限:

  
  
  
  

  

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号