模拟版本更新
来源:互联网 发布:手机淘宝差评如何删除 编辑:程序博客网 时间:2024/06/05 15:48
package com.bb.banben;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import com.bwie.banben.bean.Config;import com.bwie.banben.bean.Version;import org.xutils.common.Callback;import org.xutils.http.RequestParams;import org.xutils.x;import java.io.File;public class MainActivity extends AppCompatActivity { private String url = "http://125.39.134.47/r/a.gdown.baidu.com/data/wisegame/7c28ac069399b336/kuaishou_4812.apk"; private ProgressDialog progressDialog;//加载框 private Callback.Cancelable cancelable;//请求任务对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDialog(); } private void initDialog() { progressDialog = new ProgressDialog(this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //取消下载任务 cancelable.cancel(); } }); } public void check(View view) throws PackageManager.NameNotFoundException { PackageManager manager = getPackageManager(); PackageInfo info = manager.getPackageInfo(getPackageName(), 0); int versionCode = info.versionCode; Version version = new Version();//自己创建的一个对象,里面封装了版本号和下载地址 version.setUrl(url); if (versionCode < version.getVersionCode()) { // File file = new File(Config.VERSION_PATH); //System.out.println("file = " + file.getAbsolutePath()); downloadApk(); } } private void downloadApk() { final RequestParams request = new RequestParams(url); request.setAutoResume(true);//设置是否支持断点下载 request.setCancelFast(true);//设置是否立即取消 //判断sdcard是否可用 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//判断sdcard存在并且可用 request.setSaveFilePath(Config.VERSION_PATH);//自己创建的一个类,里面存放了下载路径public static final String VERSION_PATH = Environment.getExternalStorageDirectory()+"/danney/versions.apk"; } cancelable = x.http().get(request, new Callback.ProgressCallback<File>() { @Override public void onSuccess(File result) { progressDialog.dismiss(); System.out.println("filepath====" + result.getAbsolutePath()); install(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public void onWaiting() { } @Override public void onStarted() { progressDialog.show(); } @Override public void onLoading(long total, long current, boolean isDownloading) { if (isDownloading) { progressDialog.setMax((int) total); progressDialog.setProgress((int) current); progressDialog.setTitle("下载进度"); System.out.println("current:" + (int) current * 100 / total); } } }); } private void install(File file) { //调用系统安装器 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "application/vnd.android.package-archive"); startActivity(intent); }}注意加网络权限和可写权限
阅读全文
0 0
- 模拟APK版本更新
- 模拟版本更新
- 模拟版本更新
- 模拟版本更新
- 版本更新
- //版本更新
- 版本更新
- 更新版本
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- php删除字符串最后一位
- 关于github上倒入项目时会出现的一些问题解析
- cenots6.5免密钥访问
- HDU 4034 Graph【最短路之floyd变形】
- Java多线程--概述-转自Kyrie lrving
- 模拟版本更新
- 如何用Java自己写一个.exe程序
- android应用开发-从设计到实现 4-8 天气预报的布局
- MongoDB实战-如何选择分片键
- [php]开发属于自己的PHP API框架(1)
- hadoop
- 结实新朋友的方法与技巧
- 资料
- Java多线程--概述-转自博客园-移动开发snow_flower