在android中连接wifi时如何触发工作管理器? [英] How to trigger work manager when wifi is connected in android?

查看:31
本文介绍了在android中连接wifi时如何触发工作管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用需要在设备连接互联网后立即同步数据.

I have app which needs to sync the data as soon as internet is connected on device.

我在 Worker 类中有同步工作,它工作正常.但是我需要在手机连接互联网后立即运行这个类.由于不推荐和弃用广播接收器的连接更改,我需要一种方法在连接互联网时启动我的课程,以便数据同步.我怎样才能做到这一点?

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?

我还想在用户存在应用程序并保持 Internet 连接状态时安排工作管理器,但是当应用程序从最近关闭时,不会调用 onDestroy.请问您对此有任何解决方案或逻辑吗?

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?

推荐答案

男孩明白了.您指出的事情是完全正确的,您应该尽量避免广播接收器,因为当今人们拥有大量应用程序,并且在连接互联网后每个应用程序触发请求都会使用户的系统冻结,因为每个应用程序都想发送请求wifi连接后.因此,Android 系统附带了 JET PACK,之后您不应执行应用程序的操作,而是向 android 系统请求该操作,它们将处理后台请求.

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.

所以你的工作将是

  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)

因此,这会给您带来好处,即您不应在互联网连接时解雇工作经理.当某些任务失败时,您只需开火.这也减少了对 android 系统的工作请求.毕竟,我们都是帮助 android 改进的社区,用户可以在手机上获得出色的体验,不会有太多滞后.希望有帮助

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

这篇关于在android中连接wifi时如何触发工作管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆