工人类内部调用BroadcastReceiver类
作者:互联网
根据我的要求,我想在Worker类内部调用BroadcastReceiver类,任何人都可以提出一些建议.
// 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)
}
解决方法:
您可以在doWork()方法内部进行调用,
override fun doWork(): Result {
val i = Intent(applicationContext, NetworkChangeReceiver::class.java)
applicationContext.sendBroadcast(i)
return Result.success()
}
标签:android-broadcast,android-workmanager,android 来源: https://codeday.me/bug/20191210/2104492.html