//获取版本
public void setVersions(Context context){
PackageManager manager=context.getPackageManager();
try {
//getPackageName()是你当前类的包名,代表是获取版本信息
PackageInfo packageInfo=manager.getPackageInfo(context.getPackageName(),PackageManager.GET_CONFIGURATIONS);
loadversion=packageInfo.versionCode;//得到现在的app版本号
getNetWork.getData(getNeworkUrl.versions, handler);
}
catch (PackageManager.NameNotFoundException n){
n.printStackTrace();
}
}
public void showNoticeDialog(Context context) {
new AlertDialog.Builder(context).setTitle("版本更新").setMessage(message)
.setPositiveButton("下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
showDownloadDialog();
}
}).setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
private double loadversion;
private String wgt;
private String jsonString;
private String message = "检测到本程序有新版本发布,建议您更新!";
public Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
jsonString = msg.getData().getString("result");
try {
JSONObject object = new JSONObject(jsonString);//字符串转换成Json
double version=object.getDouble("ver");
wgt=object.getString("wgt");
if(loadversion<version){
showNoticeDialog(context);
}
else{
Toast.makeText(context, "已是最新版本", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.printStackTrace();
}
}
};
private boolean isCancel;
private ProgressBar progressBar;
private Dialog dialog;
//下载进度
public void showDownloadDialog(){
View view= LayoutInflater.from(context).inflate(R.layout.down_prograss,null);
progressBar=(ProgressBar)view.findViewById(R.id.down_progressBars);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("软件版本更新");
builder.setView(view);
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
isCancel = true;
}
});
dialog = builder.create();
dialog.show();
downloadApp();
}
/**
* 下载
*/
public void downloadApp(){
new Thread(new Runnable() {
@Override
public void run() {
URL url=null;
HttpURLConnection connection;
FileOutputStream out = null;
InputStream in = null;
try {
url=new URL(wgt);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
long fileLength = connection.getContentLength();//获取apk文件的字节数
in=connection.getInputStream();//获取输入流
File filePath =new File(FILE_PATH);
if(!filePath.exists()) {
filePath.mkdir();
}
out = new FileOutputStream(new File(FILE_NAME));
byte[]buffer=new byte[1024];
int len=0;
long readedLength=01 ;
while ((len=in.read(buffer))!=-1){
// 用户点击“取消”按钮,下载中断
if(isCancel) {
break;
}
out.write(buffer,0,len);
readedLength += len;
curProgress = (int) (((float) readedLength / fileLength) * 100);
handlers.sendEmptyMessage(UPDARE_TOKEN);
if(readedLength >= fileLength) {
dialog.dismiss();
// 下载完毕,通知安装
handlers.sendEmptyMessage(INSTALL_TOKEN);
break;
}
}
out.flush();//刷新
out.close();//关闭输出
in.close();//关闭输入流
connection.disconnect();//中断连接
}
catch (Exception e){
e.printStackTrace();
Log.i("zy", wgt + "地址");
}
}
}).start();
}
public final Handler handlers = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDARE_TOKEN:
progressBar.setProgress(curProgress);
break;
case INSTALL_TOKEN:
installApp();//安装
break;
}
}
};
/**
* 安装新版本应用
*/
public void installApp() {
File appFile = new File(FILE_NAME);
if(!appFile.exists()) {
return;
}
// 跳转到新版本应用安装页面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + appFile.toString()), "application/vnd.android.package-archive");
context.startActivity(intent);
}
Android 应用升级
最新推荐文章于 2023-05-29 15:34:03 发布