在 Worker class 内部调用 BroadcastReceiver class

Inside of Worker class call BroadcastReceiver class

我的需求想在Worker内部调用 class 调用 BroadcastReceiver class ,请大家给点建议。

// 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()
}