public class MainActivity extends AppCompatActivity { private int code; private ProgressDialog progressDialog; private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWebView(); PackageManager manager = getPackageManager(); try { PackageInfo info = manager.getPackageInfo(getPackageName(), 0); code = info.versionCode; showUpate(); } catch (Exception 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 showUpate(){ 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 pross= (int) (current/total*100); if (pross>=0&&pross<=100){updataProgressDialog(pross); }}
});
}
//安卓开发 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();
}Application 类
public class App extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); } } //网址![]()
http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk
下载安装apk 与webview 百度界面
最新推荐文章于 2025-07-24 17:06:19 发布