使用Android中的JobSchedulers检测网络状态更改 [英] Detect Network State change using JobSchedulers in Android

查看:415
本文介绍了使用Android中的JobSchedulers检测网络状态更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android N上,您无法为CONNECTIVITY_CHANGE意向静态注册广播接收器.

With Android N, You cannot statically register a Broadcast receivers for CONNECTIVITY_CHANGE intent.

来自 http://developer.android.com/preview /features/background-optimization.html#connectivity-action Google文档建议使用Job Schedulers来执行此任务.

From http://developer.android.com/preview/features/background-optimization.html#connectivity-action Google documentation suggest using Job Schedulers to perform this task.

是否可以使用Android中的Job Scheduler检测网络状态变化(从LTE到wifi),反之亦然?

Is it possible to detect network state change (LTE to wifi) and vice versa using Job Schedulers in Android?

推荐答案

是,不是.

JobInfo.Builder.setRequiredNetworkType()方法允许您安排作业在满足特定网络条件时运行.

The JobInfo.Builder.setRequiredNetworkType() method allows you to schedule jobs to run when specific network conditions are met.

网络类型可以是以下三个值之一:

The network type can be one of three values:

  • JobInfo.NETWORK_TYPE_NONE:不需要网络连接.
  • JobInfo.NETWORK_TYPE_UNMETERED:未计量的WiFi或以太网连接.
  • JobInfo.NETWORK_TYPE_ANY:任何网络连接(WiFi或蜂窝网络).
  • JobInfo.NETWORK_TYPE_NONE: No network connectivity required.
  • JobInfo.NETWORK_TYPE_UNMETERED: An unmetered WiFi or Ethernet connection.
  • JobInfo.NETWORK_TYPE_ANY: Any network connection (WiFi or cellular).

现在,抓住了……没有NETWORK_TYPE_CELLUAR.您的应用只能在蜂窝上 时才能唤醒. (为什么要这么做?)

Now, the catch... there's no NETWORK_TYPE_CELLUAR. You can't have your app only wake up when it's only on cellular. (Why would you want to do that?)

其他功能... WiFi连接可以计量或不计量.计费连接通常是移动热点之类的东西,可以自动检测到该热点(热点可以发送特殊的DHCP选项),或者用户可以在每个网络的WiFi设置中手动切换它.

The other catch... WiFi connections can be metered or unmetered. Metered connections are typically things like mobile hotspots, and this can either be automatically detected (there's a special DHCP option the hotspot can send), or the user can manually toggle it on a per-network basis from WiFi Settings.

因此,是的,您可以在JobScheduler作业上设置网络类型的约束.但是,不,您没有获得所需的粒度级别.

So, yes, you can set network-type constraints on your JobScheduler job. However, no, you don't get the level of granularity you're asking for.

正如@CommonsWare提到的那样,除非您有充分的理由,否则您通常希望安排在与网络无关的情况下进行与网络相关的作业. (将作业推迟到可用交流电源之前也是个好主意,使用setRequiresCharging(true)可以节省电池.)

As @CommonsWare mentioned, the idea is that you usually want to schedule network-related jobs to occur when network connectivity is unmetered, unless you have a good reason. (It's also a good idea to defer jobs until AC power is available, using setRequiresCharging(true), to conserve battery.)

这篇关于使用Android中的JobSchedulers检测网络状态更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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