我正在设置带有约束的OneTimeWorkRequest - “ setRequiresCharging ”但是当我将电池状态切换为放电充电时,此工作请求不会调用doWork方法。
这个问题存在于模拟器上并在我的真实设备上正常工作,因此我认为它可能无法在所有设备上运行。
public class MainActivity extends AppCompatActivity { WorkManager mWorkManager; public static final String TAG="MainActivityTag"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWorkManager = WorkManager.getInstance(); setupWorkRequest(); } private void setupWorkRequest() { Log.d(TAG, "setupWorkRequest: " + "setting request"); // Create charging constraint Constraints constraints = new Constraints.Builder() .setRequiresCharging(true) .build(); OneTimeWorkRequest chargerListen = new OneTimeWorkRequest .Builder(ChargerListener.class) .setConstraints(constraints) .build(); mWorkManager.enqueue(chargerListen); }}
public class ChargerListener extends Worker { private static final String TAG = "ChargerListenerTag"; public ChargerListenerWork(Context context, WorkerParameters workerParams){ super(context, workerParams); } @NonNull @Override public Result doWork() { Log.d(TAG, "doWork: "+"Charger Connected"); // do some work when connected return Result.success(); }}
在logcat我希望充电器连接,它可以在我的三星设备上工作,但不能在Android模拟器上工作。
交互式爱情
相关分类