Files
NutritionMaster/todaystepcounterlib/src/main/java/com/today/step/lib/TodayStepManager.java
ScorpioMiku c57388dc94 计步ok了
2018-08-28 13:31:46 +08:00

64 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.today.step.lib;
import android.app.Application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
/**
* 计步SDK初始化方法
* Created by jiahongfei on 2017/10/9.
*/
public class TodayStepManager {
private static final String TAG = "TodayStepManager";
private static final int JOB_ID = 100;
/**
* 在程序的最开始调用最好在自定义的application oncreate中调用
*
* @param application
*/
public static void init(Application application) {
StepAlertManagerUtils.set0SeparateAlertManager(application);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// initJobScheduler(application);
}
startTodayStepService(application);
}
public static void startTodayStepService(Application application) {
Intent intent = new Intent(application, TodayStepService.class);
application.startService(intent);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static void initJobScheduler(Application application) {
Logger.e(TAG, "initJobScheduler");
JobScheduler jobScheduler = (JobScheduler)
application.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(
JOB_ID,
new ComponentName(application.getPackageName(), JobSchedulerService.class.getName()));
builder.setMinimumLatency(5000)// 设置任务运行最少延迟时间
.setOverrideDeadline(60000)// 设置deadline若到期还没有达到规定的条件则会开始执行
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)// 设置网络条件
.setRequiresCharging(true)// 设置是否充电的条件
.setRequiresDeviceIdle(false);// 设置手机是否空闲的条件
int resultCode = jobScheduler.schedule(builder.build());
if (JobScheduler.RESULT_FAILURE == resultCode) {
Logger.e(TAG, "jobScheduler 失败");
}
}
}