Android work


    public void click4(View view) {
        startWork();
    }

    public void click5(View view) {
        startWork3();
    }

    public void click6(View view) {

    }

    // https://www.cnblogs.com/endv/p/13403988.html
    private void startWork1() {
        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(MyWork.class)//一次性Work请求
                .setInitialDelay(10, TimeUnit.SECONDS)//初始延迟10秒
                .build();
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);//添加到WorkManager队列中
    }

    private void startWork2() {
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);

        Data data = new Data.Builder().putString("putData", "输入数据123" + "," + formattedDateTime).build();//创建需要传入的数据,注意不支持序列化数据传入
        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(MyWork.class)//一次性Work请求
                .setInitialDelay(2, TimeUnit.SECONDS)
                .setInputData(data)
                .build();
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);//添加到WorkManager队列中
    }

    OneTimeWorkRequest oneTimeWorkRequest = null;

    private void startWork() {
        Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
        oneTimeWorkRequest = new OneTimeWorkRequest.Builder(MyWork.class).setConstraints(constraints).build();
        WorkManager.getInstance(MainActivity.this).getWorkInfoByIdLiveData(oneTimeWorkRequest.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                switch (workInfo.getState()) {
                    case BLOCKED:
                        Log.e("调试_临时_log", "堵塞");
                        Toast.makeText(getApplicationContext(), "堵塞", Toast.LENGTH_SHORT).show();
                        break;
                    case RUNNING:
                        Log.e("调试_临时_log", "正在运行");
                        Toast.makeText(getApplicationContext(), "正在运行", Toast.LENGTH_SHORT).show();
                        break;
                    case ENQUEUED:
                        Log.e("调试_临时_log", "任务入队");
                        Toast.makeText(getApplicationContext(), "任务入队", Toast.LENGTH_SHORT).show();
                        break;
                    case CANCELLED:
                        Log.e("调试_临时_log", "取消");
                        Toast.makeText(getApplicationContext(), "取消", Toast.LENGTH_SHORT).show();
                        break;
                    case FAILED:
                        Log.e("调试_临时_log", "失败");
                        Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                        break;
                    case SUCCEEDED:
                        Log.e("调试_临时_log", "成功");
                        Log.e("调试_临时_log", "this_data = " + workInfo.getOutputData().getString("data"));
                        Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);// 添加到WorkManager队列中
    }

    private void startWork3() {
        try {
            WorkInfo workInfo = WorkManager.getInstance(MainActivity.this).getWorkInfoById(oneTimeWorkRequest.getId()).get();
            Log.e("调试_临时_log", "this_" + workInfo.getState());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

posted @ 2024-08-25 02:08  古兴越  阅读(8)  评论(0编辑  收藏  举报