I have app which needs to sync the data as soon as internet is connected on device.
I have the sync work in Worker class and it is working fine. But I need to run this class as soon as internet is connected in phone. As broadcast receiver's on connection change is not recommended and deprecated, I need a way to fire up my class when internet is connected so that the data gets synced. How can I achieve this?
I also thought to schedule the work manager when user exists the app and keep condition of Internet connected but when app is closed from recent the onDestroy is not called. Do you have any solution or logic for this please?
WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts.
Stay organized with collections Save and categorize content based on your preferences. To get started using WorkManager, first import the library into your Android project. Once you've added the dependencies and synchronized your Gradle project, the next step is to define some work to run.
android.net.wifi.WifiManager. This class provides the primary API for managing all aspects of Wi-Fi connectivity. On releases before Build. VERSION_CODES. N , this object should only be obtained from an application context, and not from any other derived context to avoid memory leaks within the calling process.
Boy got your point. The thing you pointed out is exactly correct that you should try to avoid the broadcast receivers for such situation as people these day has large number of apps and each app firing request after internet is connected would make user's system freeze as each app want to send request after wifi is connected. Thus android system came with JET PACK after which you should not perform your app's action but request that action to android system and they will handle background request.
As Saeed mentioned above go with
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest onetimeJob = new OneTimeWorkRequest.Builder(YourJob.class)
.setConstraints(constraints).build(); // or PeriodicWorkRequest
WorkManager.getInstance().enqueue(onetimeJob);
But wait, you want to fire the work when internet is connected so you want some kinda thing like deprecated broadcast for connection change. Why don't you do one thing? Whenever the data you get when user is in the foreground or background use fire the work manager. If you are using retrofit for it then on it returns error when there is no internet connection thus you can schedule job when the failure is due to network.
So you work will be
override fun onFailure(call: Call<Chat>, t: Throwable) {
Log.v("chatsyncchecking","sending message failed",t)
if(t is IOException){
Log.v("chatsyncchecking","scheduling chat sync")
(app as App).enqueueChatSync()
}
}
(You can fire every request from application class)
So this make you a benefit that you should not fire work manager whenever the internet is connected. Just you fire when some task fail. This makes less work request to android system too. After all we all are the community to help android improve and users to have great experience with phone no lagging much. Hope it helps
Due to the new restrictions on Android O and above, You can't fire up your class immediately after the network connection is active using manifest flags. But you can tell Workmanager
to run your job as soon as any internet connection is available.
To do that you need to define a constraint:
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
And then enqueue your job:
OneTimeWorkRequest onetimeJob = new OneTimeWorkRequest.Builder(YourJob.class)
.setConstraints(constraints).build(); // or PeriodicWorkRequest
WorkManager.getInstance().enqueue(onetimeJob);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With