'How to work with Worker.class from the outside
I have a Worker class which work with WorkManager, and i can't take something from that class. In my case it's implement LocationListener and in onLocationChange() method I get Location, so I need translate this location in any other class (in Activity exactly, but nvm).
Becouse class created inside PeriodicWorkRequest -> I don't have instance of it and can't interact with him.
How people interacting with kind of class like this?
MainActivity.java
public class WorkView extends AppCompatActivity
implements WorkViewInterface, LocationPermissionInterface {
private ActivityMainBinding mainBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(mainBinding.getRoot());
WorkManager.getInstance(this).enqueue(new PeriodicWorkRequest
.Builder(ForegroundWorker.class, 10, TimeUnit.MINUTES, 15, TimeUnit.MINUTES)
.build());
}
}
Worker.class
public class ForegroundWorker extends Worker
implements LocationListener {
@Override
public Result doWork() {
locationManager.requestSingleUpdate
(LocationManager.NETWORK_PROVIDER,
this, Looper.getMainLooper());
}
@Override
public void onLocationChanged(@NonNull Location location) {
passLocationSomewhere(location);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
