下载安卓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);
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值