本文介绍了如何在 Android 中监控网络连接状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个正在从服务器下载文件的服务.我警告用户在下载开始之前只能通过无线局域网下载文件.
I have a Service that is downloading a file from a server. I warn the user to only download the file over Wireless LAN before the download starts.
我的问题是,如果我断开网络连接,我的下载就会卡住.有没有办法监听网络连接的变化或者它是否完全丢失?
My problem is that my download gets stuck if I loose the network connection. Is there a way to listen for changes in network connection or if it is lost entirely?
推荐答案聆听 CONNECTIVITY_ACTION
这看起来不错 示例代码.这是一个片段:
This looks like good sample code. Here is a snippet:
BroadcastReceiver networkStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.w("Network Listener", "Network Type Changed"); } }; IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(networkStateReceiver, filter);更多推荐
如何在 Android 中监控网络连接状态?
发布评论