xUtils 下载apk , 实现断点续传

来源:互联网 发布:db2还原数据库指令 编辑:程序博客网 时间:2024/06/01 15:33

添加Button点击按钮,点击按钮开始使用xUtils的网络模块进行apk安装包的下载,支持随时停止下载任务,成功后使用Dialog询问用户是否安装,并安装。

数据接口:

http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk



代码实现:


导入xUtils 的依赖或jar包


MyApp.java

public class MyApp extends Application {    @Override    public void onCreate() {        super.onCreate();        x.Ext.init(this);        x.Ext.setDebug(true);    }}

在清单文件中引入, 并且添加 网络权限和sd卡的读写权限


<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><application    android:name=".demo01.MyApp"


页面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="bw.com.bw_test.demo01.DownLoadApkActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="下载"        android:id="@+id/but_id"        />    <ProgressBar        android:layout_width="match_parent"        android:layout_height="5dp"        android:id="@+id/progress_bar_id"        android:layout_marginTop="30dp"        style="?android:attr/progressBarStyleHorizontal"        /></LinearLayout>

代码:


@ContentView(value = R.layout.activity_down_load_apk)public class DownLoadApkActivity extends AppCompatActivity {    @ViewInject(value = R.id.but_id)    private Button button;    @ViewInject(value = R.id.progress_bar_id)    private ProgressBar pb;    private Callback.Cancelable cancelable;    private boolean isDownLoading = true;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        x.view().inject(this);    }    @Event(value = {R.id.but_id})    private void onClick(View view)    {        if (isDownLoading==true)        {            button.setText("暂停");            RequestParams params = new RequestParams("http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk");            params.setAutoResume(true);            params.setSaveFilePath(getExternalCacheDir()+ File.separator+"abc.apk");            cancelable =   x.http().get(params, new Callback.ProgressCallback<File>() {                @Override                public void onLoading(long total, long current, boolean isDownLoading) {                    pb.setProgress((int)(current*100/total));                }                @Override                public void onSuccess(final File file) {                    AlertDialog.Builder builder = new AlertDialog.Builder(DownLoadApkActivity.this);                    builder.setIcon(R.mipmap.ic_launcher);                    builder.setTitle("提示");                    builder.setMessage("是否立即安装?");                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            Intent intent = new Intent();                            intent.setAction(Intent.ACTION_VIEW);                            intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                            DownLoadApkActivity.this.startActivity(intent);                        }                    });                    builder.setNegativeButton("取消",null);                    builder.show();                }                @Override                public void onError(Throwable throwable, boolean b) {                }                @Override                public void onCancelled(CancelledException e) {                }                @Override                public void onFinished() {                }                @Override                public void onWaiting() {                }                @Override                public void onStarted() {                }            });        }else        {            button.setText("下载");            cancelable.cancel();        }        isDownLoading = !isDownLoading;    }}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 广州南到珠海高铁时刻表 珍珠项链 瑚组词 瑚组词语 瑚琏 瑚怎么组词 瑚的组词 瑚琏之器 鸦瑚 瑚拼音 瑚字组词 红楼之瑚爷不好惹 瑚组词语有哪些词语 酒醉的瑚蝶 红楼之瑚不归 乱世仙妻 十瑚 瑚的组词有什么 脸上长瑚蝶斑怎么办 酒醉的瑚碟 瑚的组词是什么 爱情呀缠缠棉如同花瑚蝶 南瑞 600406国电南瑞 国电南瑞 股票国电南瑞 国电南瑞股票 南瑞国电 国电南瑞怎么样 国电南瑞股票分析 国电南瑞股吧 国电南端 南瑞继保 南瑞集团 600406 南瓜饼做法 南瓜做法 南瓜功效 南瓜粥 南瓜馒头 南瓜影视