关闭

xUtils断点续传

94人阅读 评论(0) 收藏 举报

代码一:

package com.example.debug;

import java.io.File;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private TextView tv_info;
    private ProgressBar pb;
    private HttpHandler handler;
    private HttpUtils http;
    private EditText edit;
    private String name;
    private String path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar) findViewById(R.id.progressBar1);
        tv_info = (TextView) findViewById(R.id.tv_info);
        edit = (EditText) findViewById(R.id.edit);
        //获取sd卡根目录
        path = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/";

        http = new HttpUtils();

        // download();
        //

        // ...
        // //调用cancel()方法停止下载
        // handler.cancel();
        // ...
    }
    String url = "http://fast.yingyonghui.com/f403cb8c098976ed0cf86655b6e95c71/56eb558b/apk/3629614/003d31176b3eb60e3ac982f29f84cebd";

    /**
     * 
     */
    private void download() {

        // TODO Auto-generated method stub
        handler = http.download(url, path + name + ".apk", true,
                new RequestCallBack<File>() {
                    // http://yinyueshiting.baidu.com/data2/music/238976199/877578151200128.mp3?xcode=021088004ed20206cf1d445645cf6d261dc7a0ae52885194",
                    // path+name+".mp3
                    @Override
                    public void onStart() {
                        System.out.println("开始下载了");
                        tv_info.setText("开始下载了");
                    }

                    @Override
                    public void onLoading(long total, long current,
                            boolean isUploading) {
                        System.out.println("正在下载:" + current + "/" + total);
                        tv_info.setText("正在下载:" + current + "/" + total);
                        pb.setMax((int) total);
                        pb.setProgress((int) current);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<File> responseInfo) {
                        System.out.println("下载成功了");
                        tv_info.setText("下载成功了");

                        File apkfile = responseInfo.result;
                        //自动安装
                        installAkp(apkfile);                    

                    }


                    @Override
                    public void onFailure(HttpException error, String msg) {
                        System.out.println("下载失败:" + msg);
                        tv_info.setText("下载失败:" + msg);
                    }
                });

    }


    private void installAkp(File apkfile) {
        if (!apkfile.exists()) {
            return;
        }
        // clearCacheData();
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
        startActivity(i);
        android.os.Process.killProcess(android.os.Process.myPid());
    }




    public void button(View view) {
        handler.cancel(false);

    }

    @SuppressLint("NewApi")
    public void newbutton(View view) {
        name = edit.getText().toString().trim();
        if (name.isEmpty()) {
            Toast.makeText(MainActivity.this, "输入文件名", 0).show();

        } else {

            download();

            SharedPreferencesUtils.saveString(MainActivity.this, "filename",
                    name);
        }

    }

    @SuppressLint("NewApi")
    public void button2(View v) {
        if (!SharedPreferencesUtils.getString(MainActivity.this, "filename")
                .isEmpty()) {
            edit.setText(SharedPreferencesUtils.getString(MainActivity.this,
                    "filename"));
        } else {
            Toast.makeText(MainActivity.this, "没有找到上次的文件名", 0).show();

        }
    }
}

代码二:

package com.example.debug;


import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {

    public static final String SP_NAME = "XD_LOGIN";
    private static SharedPreferences sp;

    public static void saveString(Context ct, String key, String value) {
        if (sp == null)
            sp = ct.getSharedPreferences(SP_NAME, 0);
        sp.edit().putString(key, value).commit();
    }

    public static String getString(Context ct, String key) {
        if (sp == null)
            sp = ct.getSharedPreferences(SP_NAME, 0);
        if (sp.getString(key, "") != null) {
            return sp.getString(key, "");
        }
        return null;
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3110次
    • 积分:176
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论