模拟版本更新

时间:2021-02-03 06:54:27
/**
* 一个逻辑非常简单的版本更新
* 在真正的项目中往往需要更多的条件限制
*/
public class MainActivity extends AppCompatActivity {

String urlVersion = "http://www.地址.com";
// String urlApk = "http://img1.gtimg.com/20/2030/203036/20303644_980x1200_0.jpg";
String urlApk = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk";
//

ProgressDialog pd ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//发起网路请求获取服务器版本号
getServierVersion(urlVersion);
}

private void getServierVersion(String url) {
new AsyncTask<String ,String, String>(){

@Override
protected String doInBackground(String... params) {
//睡眠3秒用来模拟 正在请求版本信息
try {
Thread.sleep(3000);

} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//在UI线程中判断 服务器版本是否比本地高 高则弹出对话框
if(CommonUtils.getVersionCode(MainActivity.this) < 2){
showDialog(CommonUtils.getVersionName(MainActivity.this),"2.1.0");
}
}

}.execute(url);
}

//弹出对话框显示更新内容
public void showDialog(String oldName, String newName){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("版本更新")
.setMessage("当前最新版本为"+newName+",手机版本为"+oldName+",是否跟新")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pd = new ProgressDialog(MainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在更新...");
pd.setMax(100);
downLoadApk(urlApk);
}
}).setNegativeButton("暂不更新", null).show();
}


private void downLoadApk(String urlApk) {
//设置下载地址
RequestParams params = new RequestParams(urlApk);
//设置下载文件的路径 开始时要注意判断 sd卡状态是否可用 本demo省略
// params.setSaveFilePath(Environment.getExternalStorageDirectory()+"/myapp/");
params.setSaveFilePath(getCacheDir()+"/myapp/");
//设置文件名
params.setAutoRename(true);
//使用ProgressCallback进行文件下载
x.http().get(params, new Callback.ProgressCallback<File>() {


@Override
public void onSuccess(File result) {
Toast.makeText(MainActivity.this,"下载成功,准备安装",Toast.LENGTH_SHORT).show();
Log.i("==========", "onSuccess: " + result);
//apk下载完成后,调用系统的安装方法 日考题 提供的apk有问题的 不能安装
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.i("==========", "onError: ");
Toast.makeText(MainActivity.this,"apk下载失败",Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
Log.i("==========", "onError: ");
pd.cancel();
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
//当前进度和文件总大小
Log.i("==========","current:"+ current +",total:"+total);

//让进度条按 XX/100的格式显示
pd.setProgress((int) (current*100/total));
pd.show();
}

});
}

}

/**
* Created by yfeng on 2017/9/16.
* 公用的工具类,可以放入一些杂七杂八的静态方法
*/

public class CommonUtils {

//获取当前应用的版本号
public static int getVersionCode(Context context){

int versionCode = 0;
try {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
versionCode = packInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;

}

//获取当前应用的版本名字
public static String getVersionName(Context context){
String versionName = "";
try {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
versionName = packInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}

}