• android 通过广播监听网络连接状况


    首先添加以下权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    new一个广播NetStateChangeBroadcast

    package aviationboss.broadcast;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo.State;
    import android.util.Log;
    import android.widget.Toast;
    import aviationboss.data.SystemData;
    
    public class NetStateChangeBroadcast extends BroadcastReceiver {
        private ConnectivityManager connectivity;
        private boolean isConnect = false;
    
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            connectivity = (ConnectivityManager) arg0
                    .getSystemService(arg0.CONNECTIVITY_SERVICE);
            State wifiState = connectivity.getNetworkInfo(
                    ConnectivityManager.TYPE_WIFI).getState();
            if (State.CONNECTED == wifiState) {
                isConnect = true;
            }
            
            if(connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)!=null){
                State mobileState = connectivity.getNetworkInfo(
                        ConnectivityManager.TYPE_MOBILE).getState();
                if (State.CONNECTED == mobileState) {
                    isConnect = true;
                }
            }
            
            Intent intent = new Intent();
            intent.setAction("netConnect");
    
            if (isConnect) {
                if (!SystemData.NetState) {
                    Toast.makeText(arg0, "网络已连接", Toast.LENGTH_SHORT).show();
                    SystemData.NetState = true;
                    intent.putExtra("netActions", "网络已连接");
                    arg0.sendBroadcast(intent);
                }
            } else {
                if (SystemData.NetState) {
                    Toast.makeText(arg0, "网络已断开", Toast.LENGTH_SHORT).show();
                    SystemData.NetState = false;
                    intent.putExtra("netActions", "网络已断开");
                    arg0.sendBroadcast(intent);
                }
            }
        }
    }

    判断wifi的连接状态

    connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()

    判断移动网的连接状态

    connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()

    在配置文件中添加该广播,并设置监听的动作

    <receiver android:name="aviationboss.broadcast.NetStateChangeBroadcast" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
  • 相关阅读:
    【bootstrapV3】移动端和PC端的 滚动监听
    【TP3.2】TP3.2的 FIND_IN_SET()的用法
    【jquery】多日期选择插件easyui date
    【onethink1.0】HTML模板获取前台和后台当前登录用户名
    【apache】phpstudy中apache 隐藏入口文件index.php (解决no input file specified错误)
    【PHP+JS】uploadify3.2 和 Ueditor 修改上传文件 大小!!
    【原创+亲测可用】JS如何区分微信浏览器、QQ浏览器和QQ内置浏览器
    【JS】移动端 好用的分享插件 soshm.js
    【TP3.2.X】linux环境下TP3.2.X的各个目录权限
    【php+微擎】微擎学习相关帮助推荐
  • 原文地址:https://www.cnblogs.com/dcxz/p/3716089.html
Copyright © 2020-2023  润新知