實現apk下載+跳轉WiFi設置頁面+xlistview加載數據

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {

    private XListView xLV;
    private int index = 1;
    private boolean flag;
    private MyBaseAdapter adapter;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //顯示當前頁面佈局
        setContentView(R.layout.activity_main);
        //獲取資源ID
        xLV = (XListView) findViewById(R.id.main_xListView);
        xLV.setPullLoadEnable(true);
        xLV.setXListViewListener(this);
        getData();
    }

    @Override
    public void onRefresh() {
        ++index;
        getData();
        flag = true;
        xLV.stopRefresh(true);
    }

    @Override
    public void onLoadMore() {
        ++index;
        getData();
        flag = false;
        xLV.stopLoadMore();
    }

    private void getData() {
        RequestParams params = new RequestParams("http://v.juhe.cn/toutiao/index?key=da161b40c11f8792fa176785dca00c0f");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MenuInFo menuInFo = gson.fromJson(result, MenuInFo.class);
                List<MenuInFo.ResultBean.DataBean> results = menuInFo.getResult().getData();
                //適配數據
                if (adapter == null) {
                    adapter = new MyBaseAdapter(results);
                    xLV.setAdapter(adapter);
                } else {
                    adapter.loadMore(results, flag);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getImage(String path, ImageView imageView) {
        //自定義配置
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//讓圖片進行內存緩存
                .cacheOnDisk(true)//讓圖片進行sdcard緩存
//                        .showImageForEmptyUri(R.mipmap.ic_empty)//圖片地址有誤
//                        .showImageOnFail(R.mipmap.ic_error)//當圖片加載出現錯誤的時候顯示的圖片
//                        .showImageOnLoading(R.mipmap.loading)//圖片正在加載的時候顯示的圖片
                .build();

        //ImageLoader.getInstance().loadImage(path,options,new Ima);//加載圖片
        //參數1:加載的圖片地址
        //參數2:將圖片設置到那個圖片控件上面
        //參數3:加載圖片配置選項,意思是指明對這張圖片的是否進行緩存(內存、sdcard)
        ImageLoader.getInstance().displayImage(path, imageView, options);
    }


    class MyBaseAdapter extends BaseAdapter {

        ImageOptions imageOptions = new ImageOptions.Builder()
                .setLoadingDrawableId(R.mipmap.ic_launcher)
                .setUseMemCache(true)
                .setSize(200, 200)
                .build();

        private List<MenuInFo.ResultBean.DataBean> results;

        public MyBaseAdapter(List<MenuInFo.ResultBean.DataBean> results) {
            this.results = results;
        }

        public void loadMore(List<MenuInFo.ResultBean.DataBean> data, boolean flag) {
            for (MenuInFo.ResultBean.DataBean bean : data) {
                if (flag) {
                    results.add(0, bean);
                } else {
                    results.add(bean);
                }
            }
        }

        @Override
        public int getCount() {
            return results != null ? results.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return results.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;

            if (convertView == null) {
                convertView = convertView.inflate(MainActivity.this, R.layout.item1_type1, null);
                holder = new ViewHolder();
                holder.finally1_title = (TextView) convertView.findViewById(R.id.finally1_title);
                holder.finally1_author_name = (TextView) convertView.findViewById(R.id.finally1_author_name);
                holder.finally1_category = (TextView) convertView.findViewById(R.id.finally1_category);
                holder.finally1_data = (TextView) convertView.findViewById(R.id.finally1_data);
                holder.finally1_image = (ImageView) convertView.findViewById(R.id.finally1_image);
                holder.finally1_pop = (ImageView) convertView.findViewById(R.id.finally1_pop);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.finally1_title.setText(results.get(position).getTitle());
            holder.finally1_author_name.setText(results.get(position).getAuthor_name());
            holder.finally1_data.setText(results.get(position).getDate());
            holder.finally1_category.setText(results.get(position).getCategory());
//            x.image().bind(holder.finally1_image, results.get(position).getThumbnail_pic_s(), imageOptions);
            getImage(results.get(position).getThumbnail_pic_s(), holder.finally1_image);

            xLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("網絡選擇");
                    builder.setNegativeButton("取消", null);
                    builder.setSingleChoiceItems(new String[]{"WIFI", "手機流量"}, 2, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                //WIFI
                                case 0:
                                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                    builder.setTitle("系統更新");
                                    builder.setMessage("現已檢測到新版本,是否更新?");
                                    builder.setNegativeButton("取消", null);
                                    builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            downLoad();
                                        }
                                    });
                                    builder.create().show();
                                    break;
                                //手機流量
                                case 1:

                                    startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));

                                    break;
                            }
                        }
                    });
                    builder.create().show();
                }
            });

            return convertView;
        }

        class ViewHolder {
            TextView finally1_title, finally1_category, finally1_author_name, finally1_data;
            ImageView finally1_image, finally1_pop;
        }

    }

    public void downLoad() {
        String wangzhi = "http://down11.zol.com.cn/suyan/lulutong3.6.5g.apk";
        String path = Environment.getExternalStorageDirectory().getPath() + "/teme1/myapk.apk";
        File file = new File(path);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdir();
        }
        RequestParams params = new RequestParams(wangzhi);
        params.setAutoRename(false);
        params.setAutoResume(true);
        params.setSaveFilePath(path);
        x.http().get(params, new Callback.ProgressCallback<File>() {
            @Override
            public void onSuccess(File result) {
                Toast.makeText(MainActivity.this, "下載成功", Toast.LENGTH_SHORT).show();
                installDownloadApk(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MainActivity.this, "下載失敗", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                cancleProgressDialog();

            }

            @Override
            public void onWaiting() {

            }

            @Override
            public void onStarted() {
                showProgressDialog();

            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                int progress = (int) (current * 100/ total );
                if (progress >= 0 && progress <= 100) {
                    updataProgressDialog(progress);
                }
            }
        });

    }

    private void installDownloadApk(File result) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
        startActivity(intent);
    }

    private void cancleProgressDialog() {
        if (progressDialog == null) {
            return;
        }
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    private void showProgressDialog() {
        progressDialog = new ProgressDialog(this);

        //設置progressDialog顯示樣式
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("我正在下載東西");
        progressDialog.setTitle("請等待");
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    private void updataProgressDialog(int progress) {
        if (progressDialog == null) {
            return;
        }
        progressDialog.setProgress(progress);
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章