下载apk


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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值