xListView+下载安装apk+跳转设置WIFI界面

来源:互联网 发布:帝国时代3兵种数据 编辑:程序博客网 时间:2024/05/23 23:27
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);    }}
原创粉丝点击