亲宝软件园·资讯

展开

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

码农向前冲 人气:0
想了解Android使用JobScheduler定期推送本地通知实例代码的相关内容吗,码农向前冲在本文为您仔细讲解Android JobScheduler定期推送的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,JobScheduler定期推送,android,本地推送通知,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和申请相关的权限:

  <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" />

加载全部内容

相关教程
猜你喜欢
用户评论