android网络侦听

//   ConnectivityManager.CONNECTIVITY_ACTION
            IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
            // filter.addAction(""); // Attribute.PAGENAME
            mContext.registerReceiver(this.broadcastReceiver, filter); // 注册

            mContext.unregisterReceiver(broadcastReceiver);

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo status = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            // NetworkInfo wifi =
            // cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (status.isConnected())
            {
                  Log.i("cm---", "网络可用");
            }
            else
            {
                  Log.i("cm---", "网络不可用");
            }
        }
    };

你可能感兴趣的:(android)