我正在制作一个通过蓝牙与pc通信的应用程序。现在我正在开发一个名为bluetoothcommworker的工人类,它包含在我的connect pc名为connectedpc的类中。我创建了worker并根据android文档将其排队。但是,当我尝试将其排队时,会得到输出和错误:
W/WM-WorkSpec: Interval duration lesser than minimum allowed value; Changed to 900000
E/WM-WorkerFactory: Could not instantiate com.example.app.ConnectedPC$BluetoothCommWorker
java.lang.NoSuchMethodException: com.example.app.ConnectedPC$BluetoothCommWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getDeclaredConstructor(Class.java:2170)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:244)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
E/WM-WorkerWrapper: Could not create Worker com.example.app.ConnectedPC$BluetoothCommWorker
这是我的密码:
package com.example.app;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import java.util.concurrent.TimeUnit;
public class ConnectedPC {
private final BluetoothDevice BLUETOOTH_DEVICE;
private boolean isActivePC;
private PeriodicWorkRequest periodicWorkRequest;
public ConnectedPC(BluetoothDevice bluetoothDevice) {
this.BLUETOOTH_DEVICE = bluetoothDevice;
}
public String getName() {
return BLUETOOTH_DEVICE.getName();
}
public boolean isActivePC() {
return isActivePC;
}
public void setActivePC(boolean activeDevice) {
isActivePC = activeDevice;
}
public String getAddress() {
return BLUETOOTH_DEVICE.getAddress();
}
public void initWork(Context context) {
Constraints constraints = new Constraints.Builder().build();
periodicWorkRequest = new PeriodicWorkRequest.Builder(
BluetoothCommWorker.class,
5,
TimeUnit.SECONDS
).setConstraints(constraints)
.setInitialDelay(2, TimeUnit.SECONDS)
.addTag(String.format("%s: BluetoothCommWorker", BLUETOOTH_DEVICE.getName()))
.build();
WorkManager.getInstance(context).enqueue(periodicWorkRequest);
}
public class BluetoothCommWorker extends Worker {
public BluetoothCommWorker(@NonNull Context context,
@NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
try {
for (int i = 0; i < 10; i++) {
System.out.println(BLUETOOTH_DEVICE.getName() + ": " + i);
SystemClock.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
return Result.failure();
}
return Result.retry();
}
}
}
dowork函数只包含一个伪for循环,因为我希望在实际处理工作代码之前先让工作代码工作。有什么解决办法吗?
1条答案
按热度按时间jrcvhitl1#
周期性工作的最小间隔为15分钟(900秒,900000毫秒)-如错误输出中所示。您应按如下方式更换单位和金额:
如果您需要更短的时间间隔,还有其他方法:
https://developer.android.com/training/scheduling/alarms
编辑:有一些技巧可以使用一次性工作人员自己重新安排时间,以更短的时间间隔运行工作
如何缩短workmanager android的时间段?
此外,您可能应该考虑前台服务来通知用户,他们有一个进程运行的任务具有微小的间隔+一些手段来停止任务,即“停止”按钮或重定向到您的应用程序,用户可以在其中停止任务:
https://developer.android.com/guide/components/foreground-services