Android 判断及监听网络连接状态

判断网络状态,并设置Diaglog提醒

方法代码


public class Tools {
    /**
     * 设置网络
     * Diaglog提醒
     * @param context
     */
    public static void checkNetWork(final Activity context) {
        if (!NetWorkStatus(context)) {
            TextView msg = new TextView(context);
            msg.setText("请设置网络!");
            AlertDialog.Builder b = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT).
                    setTitle("没有可用的网络")
                    .setMessage("是否对网络进行设置?");
            b.setPositiveButton("是", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //跳转到设置网络界面
                    context.startActivity(new Intent(Settings.ACTION_SETTINGS));
                    context.finish();
                }
            }).setNeutralButton("否", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                    context.finish();
                }
            }).create().show();
        }
    }

    /**
     * 判断网络状态
     */
    public static boolean NetWorkStatus(Context context) {
        //获取系统的连接服务
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }
}

判断网络类型

info.getType()==ConnectivityManager.TYPE_WIFI;//判断WIFI网

info.getType()==ConnectivityManager.TYPE_MOBILE;//判断网移动网络

Avtivity中直接调用 Tools.checkNetWork(this)即可。

监听网络状态,并设置Diaglog提醒

方法代码

public class NetState extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent arg) {
        ConnectivityManager manager = (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (!gprs.isConnected() && !wifi.isConnected()) {
                AlertDialog.Builder ab = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
                ab.setTitle("网络不可用");
                ab.setMessage("请检查并设置网络!无网络会导致程序部分联网功能不可用。是否对现在网络进行设置?");
                ab.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //跳转到设置网络界面
                        context.startActivity(new Intent(Settings.ACTION_SETTINGS));
                        dialog.dismiss();
                    }
                }).setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.cancel();

                    }
                }).create().show();

            }
        }
    }
}
public class Tools {
    public static void monitoringNetwork(Context context, NetState receiver) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(receiver, filter);
        receiver.onReceive(context, null);
    }
    }

在Activity中监听

public class QRCodePayActivity extends Activity {

    private NetState receiver = new NetState();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_pay);    
        Tools.monitoringNetwork(this,receiver);
    }

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

你可能感兴趣的:(Android笔记)