(Original) A network setting tool class

Encapsulates a simple network tool class

Can realize simple network monitoring

Determine whether the network is connected

Enter the settings to open the network

Get the type of network connection (1: WIFI network 2: wap network 3: net network)

The specific code is as follows

public class NetUtil {

    /**
     * 网络连通状态
     */
    public static void startDialogNetSetting(final Context context, String message) {
        try {
            boolean online = isNetAvailable(context);
            if (!online) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT);
                builder.setTitle(context.getResources().getString(R.string.tip));
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setMessage(message);
                builder.setPositiveButton(context.getResources().getString(R.string.net_settings),
                        new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,
                            int which) {
                        startNetSetting(context);
                    }
                });

                builder.setNegativeButton(context.getResources().getString(R.string.exit),
                        new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,
                            int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog dlg = builder.create();
                dlg.show();
            } else {

            }
        } catch (Exception e) {
            System.out.println("faild to show net setting");
        }
    }

    /**
     * 进入网络设置
     */
    private static void startNetSetting(Context context) {
        try {
            context.startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
        } catch (Exception e) {
            System.out.println("failed to startNetSetting");
        }
    }

    public static boolean isNetAvailable(Context context) {
        ConnectivityManager cManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cManager == null) {
            return false;
        } else {
            NetworkInfo[] info = cManager.getAllNetworkInfo();//获取设备支持的所有网络类型的链接状态信息
            if (info != null) 
            {
                for (int i = 0; i < info.length; i++) 
                {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) 
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    public static final int NoNetWork = -1;
    public static final int WiFiSNet = 1;
    public static final int MOBILENet = 2;
  //返回值 -1:没有网络  1:WIFI网络2:wap网络3:net网络
    public static int GetNetype(Context context)
    {
    	   int netType = -1;    
    	    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);   
    	    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();   
    	    if(networkInfo==null)  
    	    {   
    	        return NoNetWork;   
    	    }   
    	    int nType = networkInfo.getType();   
    	    if(nType==ConnectivityManager.TYPE_MOBILE)  
    	    {   
    	        if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet"))  
    	        {   
    	            netType = MOBILENet;   
    	        }   
    	        else  
    	        {   
    	            netType = MOBILENet;   
    	        }   
    	    }   
    	    else if(nType==ConnectivityManager.TYPE_WIFI)  
    	    {   
    	        netType = WiFiSNet;   
    	    }   
    	    return netType; 
    }
}

 

Guess you like

Origin blog.csdn.net/Android_xiong_st/article/details/110222265