public class MainActivity extends AppCompatActivity {
private int versionCode;
private ProgressDialog progressDialog;
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWebView();
PackageManager packageManager = getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
versionCode = packageInfo.versionCode;
showUpdate();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
private void initWebView() {
String url = "http://www.baidu.com";
webView = (WebView) findViewById(R.id.web_view);
//设置该方法,在当前app中显示网页
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
});
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
private void showUpdate() {
new AlertDialog.Builder(this)
.setTitle("更新版本")
.setMessage("当前版本是" + versionCode + " 需要升级")
.setPositiveButton("下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadApk();
}
})
.setNegativeButton("取消", null)
.create()
.show();
}
private void downloadApk() {
String url = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk";
String path = Environment.getExternalStorageDirectory().getPath() + "/teme1/myapk.apk";
File file = new File(path);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdir();
}
RequestParams params = new RequestParams(url);
params.setAutoRename(false);
params.setAutoResume(true);
//设置保存路径
params.setSaveFilePath(path);
x.http().get(params, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
installDownloadApk(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
cancleProgressDialog();
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
showProgressDialog();
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
int progress = (int) (current / total * 100);
if (progress >= 0 && progress <= 100) {
updataProgressDialog(progress);
}
}
});
}
//安卓开发 apk安装
private void installDownloadApk(File result) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
startActivity(intent);
}
private void cancleProgressDialog() {
if (progressDialog == null) {
return;
}
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
private void updataProgressDialog(int progress) {
if (progressDialog == null) {
return;
}
progressDialog.setProgress(progress);
}
private void showProgressDialog() {
progressDialog = new ProgressDialog(this);
//设置progressDialog显示样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("我正在下载东西");
progressDialog.setTitle("请等待");
progressDialog.setProgress(0);
progressDialog.show();
}
}