在 firebase 作业调度程序中,可以检测我们何时连接到网络,但是如何检测我们何时断开网络连接?
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-unique-tag")
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
// only run when the device is charging
Constraint.DEVICE_CHARGING
)
.build();
dispatcher.mustSchedule(myJob);
这将检测何时连接到 Unmetrered Network ,我想在与所有网络断开连接时启动一项服务
使用 firebase 调度程序,您无法检查断开连接(据我所知),但您可以使用广播接收器来检测连接更改并在其中调用您的作业服务
请参阅此链接了解广播接收器的使用
用于检查 Android 应用程序中的互联网连接的广播接收器 https://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app
在最新版本的 android 中,您需要动态注册和取消注册广播接收器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)