XUtils3.0使用后续

HttpUtils使用:

public void btnHttpImage(View view) {
        String uri = "https://img-my.csdn.net/uploads/201407/26/1406382765_7341.jpg";
        RequestParams entity = new RequestParams(uri);
        x.http().get(entity, new Callback.CommonCallback<byte[]>() {
            @Override
            public void onSuccess(byte[] result) {
                Log.d("flag", "------------------->onSuccess");
                if (result != null) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(result,0,result.length);
                    mImageView.setImageBitmap(bitmap);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("flag", "------------------->onError");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

使用xUtils下载视频等比较大的资源

public void btnDownload(View view) {
        String url = "http://vf1.mtime.cn/Video/2015/03/20/mp4/150320094140850937_480.mp4";
        final String target = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .getAbsolutePath() + File.separator + "ab.mp4";

        RequestParams entity = new RequestParams(url);

        entity.setSaveFilePath(target);
//        设置断点续传,当下载因为网络原因中断时,下次再次下载时,从上次下载的位置接着下载
        entity.setAutoResume(true);
//        设置联网超时时间
        entity.setConnectTimeout(5000);
//        设置自动重命名
        entity.setAutoRename(true);

//        设置重试次数
        entity.setMaxRetryCount(3);
//        设置优先级
        entity.setPriority(Priority.BG_TOP);

//        设置缓存失效时间
        entity.setCacheMaxAge(1000*60);

        Toast.makeText(this,"开始下载",Toast.LENGTH_LONG).show();

        Callback.Cancelable cancelable = x.http().get(entity, new Callback.ProgressCallback() {

            ProgressDialog dialog = null;
            @Override
            public void onWaiting() {
                Log.d("flag", "------------------>onWaiting");
            }

            @Override
            public void onStarted() {
                Log.d("flag", "-------------->onStart()");

                dialog= new ProgressDialog(DownloadActivity.this);

                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                dialog.setIcon(R.mipmap.download);

                dialog.setMessage("正在拼命下载美国队长……");

                dialog.setTitle("下载中");

                dialog.setProgress(0);

                dialog.show();

            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                int percent = (int) (100 * current / total);

                Log.d("flag", "------------->percent: " + percent);

                dialog.setProgress(percent);

            }
            @Override
            public void onSuccess(File result) {
                Log.d("flag", "----------------->onSuccess");

                if(dialog.isShowing()){
                    dialog.dismiss();
                }

//                发送广播通知用户,下载完成是否播放
                sendMyBroadcast(target);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("flag", "--------------->onError");
                if(dialog.isShowing()){
                    dialog.dismiss();
                }

            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.d("flag", "----------------->onCancel");
                if(dialog.isShowing()){
                    dialog.dismiss();
                }

            }

            @Override
            public void onFinished() {
                if(dialog.isShowing()){
                    dialog.dismiss();
                }
            }
        });

    }

发送广播的方法:

    private void sendMyBroadcast(String target) {
        Intent intent = new Intent();

        intent.setAction("com.softpo.message");

        intent.putExtra("path",target);

        sendBroadcast(intent);
    }

广播中处理代码

 @Override
    public void onReceive(final Context context, Intent intent) {
        String action = intent.getAction();

        if("com.softpo.message".equals(action)){
//            获取文件地址
            final String path = intent.getStringExtra("path");

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

            builder.setTitle("播放视频");

            builder.setMessage("是否播放大片?");

            builder.setIcon(R.mipmap.captain3);

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

            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Intent intent1 = new Intent(Intent.ACTION_VIEW);

                    File file = new File(path);
                    Uri uri = Uri.fromFile(file);

                    Uri parse = Uri.parse("file://" + path);
                    intent1.setDataAndType(parse,"video/*");

                    context.startActivity(intent1);

                }
            });
            builder.show();
        }
    }

发送广播后效果如图所示:
XUtils3.0使用后续_第1张图片

xUtils中数据库的使用

大家可以下载这个综合Demo(xUtils四部分都包含)进行详细学习,这个工程用的是Android Studio2.0
下载地址是:
http://download.csdn.net/detail/soft_po/9541863

你可能感兴趣的:(Android,开发框架)