工人类内部调用BroadcastReceiver类

前端之家收集整理的这篇文章主要介绍了工人类内部调用BroadcastReceiver类 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@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)
}
最佳答案
您可以在doWork()方法内部进行调用,

@H_404_1@

override fun doWork(): Result {
     val i = Intent(applicationContext,NetworkChangeReceiver::class.java)
     applicationContext.sendBroadcast(i)

     return Result.success()
}
原文链接:https://www.f2er.com/android/531407.html

猜你在找的Android相关文章