工具程式碼---版本更新
阿新 • • 發佈:2018-11-13
第一步獲取當前版本號
public static String getVersionNow(Context con){
PackageManager packageManager = con.getPackageManager();
PackageInfo packInfo = null;
try {
packInfo = packageManager.getPackageInfo(con.getPackageName(),0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return packInfo.versionName;
}
-
-
第二步 獲取最新版本號
第三步 彈出對話方塊提示下載更新
public static void AlertDialog(Context con,String versionName, String versionName_new,final DialogPosNav dia) {
final android.app.AlertDialog.Builder b=new Builder(con);
b.setTitle("版本更新" );
b.setMessage("當前版本是"+versionName+" 最新版本是"+versionName_new+"確定要更新嗎?");
b.setPositiveButton("確定", new android.content.DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
android.app.AlertDialog dialog = b.create();
//downLoadApk();
dialog.dismiss();
dia.positive(); //介面回撥 更新介面回撥
}
});
b.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
android.app.AlertDialog dialog = b.create();
dialog.dismiss();
//positive();
dia.negative();//介面回撥 不更新介面回撥
}
});
b.show();
}
//定義一個介面 用於點選事件方法的回撥
public interface DialogPosNav{
public void positive();
public void negative();
}
第四步 下載
public static void downLoadApk(final Context con,String url) {
pd = new ProgressDialog(con);
pd.setTitle("正在下載更新");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
final String uu=url.trim();
System.out.println("下載的地址-------------------------"+uu);
new Thread(new Runnable() {
@Override
public void run() {
try {
File file = getFileFromServer(con,uu,pd);//http://download.mgsit.com.cn/download/multigold15.04.02.apk
Thread.sleep(3000);
pd.dismiss();
if(file!=null){
installAPK(file,con);
}else{
}
} catch (InterruptedException e) {
ToastUtils.Toas(con, "下載失敗,請下次再試");
e.printStackTrace();
}
}
}).start();
}
/**
* 從伺服器獲取更新的檔案
* @param path 路徑
* @param pd 進度條
*/
protected static File getFileFromServer(Context con,String path, ProgressDialog pd) {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //sd卡正常
try {
URL url = new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
pd.setMax(conn.getContentLength());
InputStream in=conn.getInputStream();
File file_apk=new File(Environment.getExternalStorageDirectory(),"updata.apk");
FileOutputStream out=new FileOutputStream(file_apk);
BufferedInputStream bs=new BufferedInputStream(in);
byte[] bt=new byte[1024];
int total=0;
int len;
while((len=bs.read(bt))!=-1){
out.write(bt, 0, len);
total+=len;
pd.setProgress(total);
}
out.close();
bs.close();
in.close();
return file_apk;
} catch (Exception e) {
ToastUtils.Toas(con, "下載新版本出錯 請稍後再試");
e.printStackTrace();
}
}else{
System.out.println("sd卡不正常");
ToastUtils.Toas(con, "sd卡不可用,請稍後再試");
return null;
}
return null;
}
/**
* 安裝apk
* @param file
*/
protected static void installAPK(File file,Context con) {
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
con.startActivity(intent);
}
第五步 使用
VersionUtils.AlertDialog(Activity_Setting.this, versionNow, versonNet, new DialogPosNav() {
@Override
public void positive() { //工具類
VersionUtils.downLoadApk(Activity_Setting.this, "url");
}
@Override
public void negative() {
}
});