下载安装升级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);
        init();
        try {
            PackageManager manager = getPackageManager();
            PackageInfo info = manager.getPackageInfo(getPackageName(),0);
            versionCode = info.versionCode;
            showUpData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void init(){
        String url = "http://www.baidu.com";
        webView = (WebView) findViewById(R.id.webview);
        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 showUpData() {
        new AlertDialog.Builder(this)
                .setTitle("更新版本")
                .setMessage("当前版本:"+versionCode+"需要升级")
                .setPositiveButton("下载", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        xiazaiAPK();
                    }
                })
                .setNegativeButton("取消",null)
                .create()
                .show();
    }
    public void xiazaiAPK(){
        String url = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk";
        String path = Environment.getExternalStorageDirectory().getPath()+"/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) {
                anzhuang(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);
                }
            }
        });


    }
    public void anzhuang(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);
    }
    public void cancleProgressDialog(){
        if (progressDialog == null) {
            return;
        }
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
    public void showProgressDialog(){
        progressDialog = new ProgressDialog(this);

        //设置progressDialog显示样式
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("我正在下载东西");
        progressDialog.setTitle("请等待");
        progressDialog.setProgress(0);
        progressDialog.show();
    }
    private void updataProgressDialog(int progress) {
        if (progressDialog == null) {
            return;
        }
        progressDialog.setProgress(progress);
    }
}
Application:
public class App  extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
    }
}

你可能感兴趣的:(下载安装升级apk)