Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger work manager when wifi is connected in android?

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?

like image 510
simplegalaxy Avatar asked Sep 29 '19 08:09

simplegalaxy


People also ask

Does Work Manager work when app is closed?

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.

How to start work Manager in Android?

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.

What is WiFi manager in Android?

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.


2 Answers

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

like image 114
Sháilèndra Wregmi Avatar answered Oct 23 '22 08:10

Sháilèndra Wregmi


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);
like image 28
Saeed Masoumi Avatar answered Oct 23 '22 08:10

Saeed Masoumi