添加Button点击按钮,点击按钮开始使用xUtils的网络模块进行apk安装包的下载,支持随时停止下载任务,成功后使用Dialog询问用户是否安装,并安装。
数据接口:
http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk
代码实现:
1,导入xUtils 的依赖
implementation 'org.xutils:xutils:3.3.40'
2,MyApp.java -- 初始化xUtils
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);//初始化xUtils
x.Ext.setDebug(true);//是否输出日志
}
}
3,在清单文件中引入, 并且添加网络权限和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"
4,页面
<?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>
5,MianActivity 代码
@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;
}
}