Android 网络监听器
我想检查Android手机的网络何时关闭。我可以捕捉该事件吗?
我没有得到正确的 API 或任何可以解释相同问题的示例。如果有人做过或任何示例链接将会非常有帮助。
I want to check when the network of phone in Android goes off. Can I capture that event?
I am not getting the proper API or any example which would explain the same. If anyone had done or any example links would be really helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
新的 java 类:
在 AndroidManifest.xml 中的“manifest”元素下新建 xml:
在 AndroidManifest.xml 中的“application”元素下新建 xml:
New java class:
New xml in your AndroidManifest.xml under the "manifest" element:
New xml in your AndroidManifest.xml under the "application" element:
我一直在使用一个小型设置来检查带宽,以确定如何缩放图像等内容。
在活动下,在 AndroidManifest 中:
在执行检查的活动中:
示例用法如下:
它不是最漂亮的,但它提供了足够的灵活性,我可以根据项目的内容和我的情况更改项目的带宽截止对他们的要求。
I have been using a small setup to check the bandwidth for determining how to scale things, such as images.
Under the activity, in AndroidManifest:
In the activity where the checks are being performed:
An example usage would then be:
It's not the prettiest, but it allows enough flexibility that I can change the bandwidth cutoff for items depending on what they are and my requirements for them.
如果您可以在活动中尝试使用 Android Annotations -就这样,剩下的就生成了:
仅当您已经使用 AndroidAnnotations 时才使用它 - 仅针对这段代码将此依赖项放入您的项目中将是过度的。
If using Android Annotations is an option for you try this in your activities - that's all, the rest is generated:
Use this only if you already use AndroidAnnotations - putting this dependency inside your project only for this piece of code would be overkill.
上述答案仅在启用移动分组数据时才有效。否则,ConnectivityManager 将为 null,并且您无法再检索 NetworkInfo。解决方法是使用 PhoneStateListener 或 TelephonyManager。
The above answer only works if mobile packet data is enabled. Otherwise, ConnectivityManager would be null and you can no longer retrieve NetworkInfo. The way around it is to use a PhoneStateListener or TelephonyManager instead.