Android work

古兴越發表於2024-08-25

    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();
        }
    }

相關文章