1. 程式人生 > >BroadcastReceiver廣播監聽

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();
    }
}