关闭

动态监听网络状态

标签: 动态监听网络android网络监广播监听网络状态
114人阅读 评论(0) 收藏 举报
分类:
    package com.pb.app;

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.RelativeLayout;
    import android.widget.Toast;

    /**
    * 相关的权限:
    * <uses-permission android:name="android.permission.INTERNET" />
    * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    */
    public class MainActivity extends Activity {
        private RelativeLayout no_net_show;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            no_net_show = (RelativeLayout) findViewById(R.id.no_net_show);

            /**
             * 网络状态改变的广播
             * 
             */
            registerBroadcast();
        }

        private void registerBroadcast() {
            // 注册网络状态改变的广播
            IntentFilter netfilter = new IntentFilter(
                    ConnectivityManager.CONNECTIVITY_ACTION);
            netfilter.addCategory(Intent.CATEGORY_DEFAULT);
            registerReceiver(receiver, netfilter);
        }

        @Override
        protected void onDestroy() {
            unregisterReceiver(receiver);
            super.onDestroy();
        }

        /**
         * 广播接收
         */
        private BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(
                        ConnectivityManager.CONNECTIVITY_ACTION)) {
                    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                    // 获得激活的网络信息
                    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
                    if(networkinfo!=null){
                        // 当前网络是否有效
                        boolean netState = networkinfo.isAvailable();
                        Log.i("caojing", "netState:"+netState);
                        // 当前网络是否已连接
                        boolean connState = networkinfo.isConnected();
                        Log.i("caojing", "connState:"+connState);
                        // 当前网络的类型 3G WIFI
                        String netType = networkinfo.getTypeName();
                        Log.i("caojing", "netType:"+netType);

                        if(netState&&connState){
                            no_net_show.setVisibility(RelativeLayout.GONE);
                            Toast.makeText(MainActivity.this, "网络连接可用,连接的为:"+netType, Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        no_net_show.setVisibility(RelativeLayout.VISIBLE);
                    }

                }
            }
        };

    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3686次
    • 积分:155
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:3篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档