1.进度条主题,有的机型右边padding失效
<style name="CustomDialog" >
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>
2.封装的工具类
public class UpDateUtils {
private static UpDateUtils upDateUtils = null;
private UpDateUtils() {}
public static UpDateUtils newInstance() {
if (upDateUtils == null) {
upDateUtils = new UpDateUtils();
}
return upDateUtils;
}
public int getVersionCode(Context context) {
PackageInfo pkg;
int versionCode = 0;
try {
pkg = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionCode = pkg.versionCode;
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return versionCode;
}
public String getVersionName(Context context) {
PackageInfo pkg;
String versionName = "";
try {
pkg = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionName = pkg.versionName;
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return versionName;
}
调用系统的安装方法
public void installAPK(Context context, File file) {
//调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data;
// 判断版本大于等于7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// "net.csdn.blog.fileprovider"即是在清单文件中配置的authorities
data = FileProvider.getUriForFile(context, "net.csdn.blog.fileprovider", file);
// 给目标应用一个临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.e("AutoUpdate","7.0data="+data);
} else {
data = Uri.fromFile(file);
Log.e("AutoUpdate","111data="+data);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(data, "application/vnd.android.package-archive");
context.startActivity(intent);
((Activity)context).finish();
}
}
3.清单文件
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="net.csdn.blog.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
4.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="download"/>
</paths>
</resources>
5.代码
private void showUpdateDialog(boolean upData, final String url) {
final String positive;
String negative;
AlertDialog alertDialog = new AlertDialog(SplashActivity.this).builder();
alertDialog.setTitle("版本更新提示");
alertDialog.setMsg("检查到新版本,是否更新?");
if (upData) {
positive = "立即更新";
negative = "稍后更新";
alertDialog.setOutCancelable(true);
}else {
positive = "确定";
negative = "取消";
alertDialog.setOutCancelable(false);
}
alertDialog.setPositiveButton(positive, new View.OnClickListener() {
@Override
public void onClick(View view) {
//去更新
String versionName = UpDateUtils.newInstance().getVersionName(SplashActivity.this);
path = Environment.getExternalStorageDirectory().getPath() + "/APP/download/" + "app_"+versionName+".apk";
Aria.download(this)
.load(url) //读取下载地址
.setFilePath(path) //设置文件保存的完整路径
.start();
progressDialog = new ProgressDialog(SplashActivity.this,R.style.CustomDialog);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("正在下载中");
progressDialog.setProgress(0);
progressDialog.setMax(100);
progressDialog.setIndeterminate(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
});
alertDialog.setNegativeButton(negative, new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("open", false);
startActivity(intent);
finish();
}
});
alertDialog.show();
}
@Download.onTaskRunning
protected void running(DownloadTask task) {
if (task.getKey().equals(url)) {
int p = task.getPercent(); //任务进度百分比
progressDialog.setProgress(p);
/* Aria.get(this).getDownloadConfig().setConvertSpeed(true);
String speed = task.getConvertSpeed(); //转换单位后的下载速度,单位转换需要在配置文件中打开*/
}
}
@Download.onTaskComplete
void taskComplete(DownloadTask task) {
//在这里处理任务完成的状态
if (progressDialog != null) {
progressDialog.dismiss();
}
UpDateUtils.newInstance().installAPK(this, new File(path));
}
@Override
protected void onDestroy() {
super.onDestroy();
Aria.download(this).unRegister();
}