我想同时打开另一个Android设备的手电筒。
例如由于硬件模块,每个android设备都会以不同的延迟打开手电筒
三星设备需要100毫秒才能打开手电筒,而华硕设备需要400毫秒才能打开手电筒
我想今天在12:30:35:430打开和关闭不同设备的闪光灯
有解决方案吗?
答案 0 :(得分:0)
如@dave所述,您可以使用WorkManager。 这是使用它的示例:
像这样创建一个工作者:
class AccountingPushWorker(context: Context, parameter: WorkerParameters) : Worker(context, parameter) {
override fun doWork(): Result {
//Do your work here
return Result.success()
}
}
这是设置运行该工作程序的时间的方法:
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR, 12)
calendar.set(Calendar.MINUTE, 30)
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1)
}
var whenTime = calendar.timeInMillis - System.currentTimeMillis()
whenTime = kotlin.math.abs(whenTime)
val oneTimeWorkRequest: OneTimeWorkRequest = OneTimeWorkRequestBuilder<AccountingPushWorker>()
.setInitialDelay(whenTime, TimeUnit.MILLISECONDS).build()
WorkManager.getInstance(activity!!).enqueueUniqueWork("one_time_work_request", ExistingWorkPolicy.KEEP, oneTimeWorkRequest)
val periodicWorkRequest: PeriodicWorkRequest = PeriodicWorkRequestBuilder<AccountingPushWorker>(24, TimeUnit.HOURS).build()
WorkManager.getInstance(activity!!).enqueueUniquePeriodicWork("periodic_work_request", ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest)
答案 1 :(得分:-2)
您尝试过WorkManager吗? WorkManager
定义任务(正在打开闪光灯)并在所需的时间执行