如何监控在Android的网络连接状态? [英] How can I monitor the network connection status in Android?

查看:143
本文介绍了如何监控在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?

推荐答案

倾听<一个href="http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION">CONNECTIVITY_ACTION

这看起来不错<一href="http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/net/NetworkConnectivityListener.java">sample code 。这里是一个片段:

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的网络连接状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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