==安卓开发判断网络==

安卓开发判断网络状态

 ------------------------工具类------------------------------------

public class NetUtils {

 

    //判断有无网络的方法

    public static boolean isConn(Context context){

        //1.得到系统服务

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        //2.得到网络信息类对象-需要添加权限

        NetworkInfo info = manager.getActiveNetworkInfo();

        //3.进行判断

        if(info!=null && info.isAvailable()){//已经连接网络

            return true;

        }else{

            return false;

        }

    }

 

    //如果没有网络的情况下,弹出一个对话框,打开设置页面

    public static void openNetDialog(final Context context){

        AlertDialog.Builder builder=new AlertDialog.Builder(context);

        builder.setTitle("提示");

        builder.setMessage("没有网络,是否进行设置?");

        builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialogInterface, int i) {

                //跳转到系统设置页面-隐士跳转

                Intent intent = null;

                // 先判断当前系统版本

                if(android.os.Build.VERSION.SDK_INT > 10){  // 3.0以上

                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);

                }else{

                    intent = new Intent();

                    intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");

                }

                context.startActivity(intent);

            }

        });

        builder.setNegativeButton("取消",null);

        AlertDialog dialog = builder.create();

        dialog.show();

    }

}

-----------------------调用----------------------

 

public class MainActivity extends AppCompatActivity {

 

    private Button button;

    private ImageView img;

 

    private Handler mHandler=new Handler(){

        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            //显示图片

            Bitmap bitmap=(Bitmap) msg.obj;

            img.setImageBitmap(bitmap);

        }

    };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        //ctrl+alt+f:设置成员变量

        button = findViewById(R.id.btndown);

        img = findViewById(R.id.img);

 

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                //进行网络判断

                boolean conn = NetUtils.isConn(MainActivity.this);

                if(conn){

                    //1.进行网络请求,属于耗时任务。。。。开启一个子线程

                    new Thread(){

                        @Override

                        public void run() {

                            //使用java.net包下的httpurlconnention

                            reqestNetData();

                        }

                    }.start();

                }else{

                    //打开对话框

                    NetUtils.openNetDialog(MainActivity.this);

                }

            }

        });

    }

 

    private void reqestNetData() {

 

        //1.创建一个Url对象 ctrl+alt+t

        try {

            URL url=new URL("https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D220/sign=7c8bf780a9cc7cd9e52d33db09012104/838ba61ea8d3fd1f91104e573a4e251f95ca5f46.jpg");

            //2. 打开连接    alt+enter自动补全  HttpURLConnection 继承自 URLConnection

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //3.进行设置

            urlConnection.setRequestMethod("GET");//设置请求方式

            urlConnection.setConnectTimeout(5000);//连接超时时间

            urlConnection.setReadTimeout(5000);//读取超时时间

 

            //4.判断响应码

            int responseCode = urlConnection.getResponseCode();

            if(responseCode==200){//正常

                //5.获取数据

                InputStream inputStream = urlConnection.getInputStream();

 

                //将流转换成二进制图片对象

                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

 

                //显示图片

                //img.setImageBitmap(bitmap);

                Message msg=Message.obtain();

                msg.obj=bitmap;

                mHandler.sendMessage(msg);

                inputStream.close();

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值