广播中判断网络

//网络状态判断工具类
public class NetWorkUtils {
   //判断网络的方法
    public static boolean isNetWork (Context context) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //需要添加网络权限
        NetworkInfo info = conn.getActiveNetworkInfo();
        if(info!=null){
            return  info.isAvailable();
        }
        return false;
    }
    //判断WiFi是否可用
    public static boolean isWifi (Context context) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //需要添加网络权限
        //NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo info = conn.getNetworkInfo(conn.TYPE_WIFI);
        if(info!=null){
            return  info.isAvailable();
        }
        return false;
    }
    //判断流量
    public static boolean isMoble (Context context) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //需要添加网络权限
        NetworkInfo info = conn.getNetworkInfo(conn.TYPE_MOBILE);
        if(info!=null){
            return  info.isAvailable();
        }
        return false;
    }
}

//
  //定义广播接收的方法
    private class NetworkBroadcas extends BroadcastReceiver {
        //重写接收广播的方法
        @Override
        public void onReceive(Context context, final Intent intent) {
            boolean b = NetWorkUtils.isNetWork(context);
            if(b){
                Toast.makeText(getActivity(), "有网", Toast.LENGTH_SHORT).show();
                getjson();
                boolean wifi = NetWorkUtils.isWifi(context);
                if(wifi){
                    Toast.makeText(getActivity(), "wifi", Toast.LENGTH_SHORT).show();
                }
                boolean moble = NetWorkUtils.isMoble(context);
                if(moble){
                    Toast.makeText(getActivity(), "手机流量大大的有", Toast.LENGTH_SHORT).show();
                }
            }else{
                Toast.makeText(getActivity(), "无网", Toast.LENGTH_SHORT).show();
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("网络提示");
                builder.setMessage("是否开启网络");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent1 = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        startActivity(intent1);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.show();
            }

        }
    }


    //通过代码动态注册广播
        NetworkBroadcas networkbroadcas =new  NetworkBroadcas();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(networkbroadcas,filter);
        
    //权限
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />





相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页