定期工作管理器始终在打开应用程序时运行。定期工作经理的行为吗?

时间:2019-05-24 03:45:55

标签: java android

WorkManager仅在首次打开应用程序时启动。但是,当我重新启动应用程序时,WorkManager总是从头开始运行,完成其收集数据的工作。是工作经理的行为吗?

用于WorkManager初始化的Class WorkManager Helper

public class WorkManagerHelper
{
    static WorkManagerHelper INSTANCE;

    public static WorkManagerHelper getInstance() {
        if(INSTANCE == null ){
            INSTANCE = new WorkManagerHelper();
        }

        return  INSTANCE;
    }

    public PeriodicWorkRequest getPeriodicWorkRequest(Class serviceClass) {

        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest
                .Builder(serviceClass, 8, TimeUnit.HOURS)
                .setConstraints(getConstraintWorkManager())
                .build();

        return periodicWorkRequest;
    }

    public OneTimeWorkRequest getOneTimeWorkRequest(Class serviceClass) {

        OneTimeWorkRequest oneTimeWorkRequest =
                new OneTimeWorkRequest.Builder(serviceClass)
                        .setConstraints(getConstraintWorkManager())
                        .setInitialDelay(15, TimeUnit.MINUTES)
                        .build();

        return oneTimeWorkRequest;
    }

    private Constraints getConstraintWorkManager() {
        Constraints constraint = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();
        return constraint;
    }

}

MainActivity.java类

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        startAllStatServices();

    }

    private void startAllStatServices()
    {
        if(SharedPrefHelper.getInstance(this).getFirstTimeOpenApp())
        {
            SharedPrefHelper.getInstance(this).setFirstTimeOpenApp();
            startInstalledAppStats();
            startRunningAppStats();
        }
    }

    private void startInstalledAppStats()
    {
        PeriodicWorkRequest periodicWorkRequest = WorkManagerHelper.getInstance().getPeriodicWorkRequest(InstalledAppCollectingDataService.class);
        WorkManager.getInstance().enqueue(periodicWorkRequest);

    }

    private void startRunningAppStats()
    {
        PeriodicWorkRequest periodicWorkRequest = WorkManagerHelper.getInstance().getPeriodicWorkRequest(RunningAppCollectingDataService.class);
        WorkManager.getInstance().enqueue(periodicWorkRequest);
    }

WorkManager RunningAppCollectingDataService

public class RunningAppCollectingDataService extends Worker
{
    private Context mContext;
    private ListRunningAppEntity listEntity = new ListRunningAppEntity();

    private final String TAG = RunningAppCollectingDataService.class.getSimpleName();

    public RunningAppCollectingDataService(@NonNull Context context, @NonNull WorkerParameters workerParams)
    {
        super(context, workerParams);
        this.mContext = context;
    }

    @NonNull
    @Override
    public Result doWork()
    {
        collectData();

        return Result.success();
    }
}

0 个答案:

没有答案