@H_404_1@根据我的要求,我想在Worker类内部调用BroadcastReceiver类,任何人都可以提出一些建议.
@H_404_1@
// BroadcastReceiver sample class
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
Log.i("??????","Working fine");
}
}
// workers class
class WorkSample(context: Context,workerParams: WorkerParameters) : Worker(context,workerParams) {
override fun doWork(): Result {
// BroadcastReceiver class
NetworkChangeReceiver()
return Result.success()
}
}
//Activity
button.setOnClickListener {
// request of workmanager
val constraints = Constraints.Builder().setrequiredNetworkType(NetworkType.CONNECTED).build()
val OneTimeWorkRequest = OneTimeWorkRequest.Builder(WorkSample::class.java).setConstraints(constraints).build()
WorkManager.getInstance().enqueue(OneTimeWorkRequest)
}
最佳答案
原文链接:https://www.f2er.com/android/531407.html