BroadcastReceiver广播监听

//    动态监听广播
    private class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean netWorkConnect = NetWorkUtils.isNetWorkConnect(context);

        if (netWorkConnect){

            Toast.makeText(context, "有网", Toast.LENGTH_SHORT).show();
            getJsonData();
        }else{

            Toast.makeText(context, "没网", Toast.LENGTH_SHORT).show();
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setIcon(R.drawable.ic_launcher_background);
            builder.setTitle("提示");
            builder.setMessage("请检查网络连接");
//            确定
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

//                    跳转到网络设置界面
                    startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                }
            });

//            取消
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });

//            显示
            builder.show();
        }
    }

//    Json解析
    private void getJsonData() {

        new Thread(new Runnable() {
            @Override
            public void run() {

//                int page = 1;
                String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1\n";
//                创建URL
                try {
                    URL url = new URL(path);
//                    Httpurl获取
                   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                   设置获取样式
                   connection.setRequestMethod("GET");
//                    设置延迟时间
//                 connection.setConnectTimeout(5000);
//                   设置编码
                    int code = connection.getResponseCode();
//                    判断是否成功
                    if (code == 200){
//                        输入流
                        InputStream stream = connection.getInputStream();
//                        输入流转输出流
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();

                        byte [] buffer = new byte[1024];
                        int len = 0;
                        while ((len = stream.read(buffer)) != -1){

                            bos.write(buffer,0,len);
                        }
//                        关流
                        stream.close();
                        bos.close();
//                        预备发送消息、
                        String json = bos.toString();
//                        消息
                        Message message = new Message();
                        message.what = 0;
                        message.obj = json;

//                        发送消息
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43971447/article/details/85376756