Android代码实现APK的下载安装和卸载<2>
项目开发使用模板:Android代码实现APK下载安装(代码依赖包afinal_0.5_bin.jar,可资源下载)
<span style="white-space:pre"> </span>protected void showUpdateDialog() { <span style="white-space:pre"> </span>//this = Activity.this <span style="white-space:pre"> </span>AlertDialog.Builder builder = new Builder(SplashActivity.this); <span style="white-space:pre"> </span>builder.setTitle("提示升级"); //<span style="white-space:pre"> </span>builder.setCancelable(false);//强制升级 <span style="white-space:pre"> </span>builder.setOnCancelListener(new OnCancelListener() { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onCancel(DialogInterface dialog) { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>//无操作或者返回就代替用户做选择,进入主页面 <span style="white-space:pre"> </span>enterHome(); <span style="white-space:pre"> </span>dialog.dismiss(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>builder.setMessage(description); <span style="white-space:pre"> </span>builder.setPositiveButton("立刻升级", new OnClickListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int which) { <span style="white-space:pre"> </span>// 下载APK,并且替换安装 <span style="white-space:pre"> </span>if (Environment.getExternalStorageState().equals( <span style="white-space:pre"> </span>Environment.MEDIA_MOUNTED)) { <span style="white-space:pre"> </span>// sdcard存在 <span style="white-space:pre"> </span>// afnal <span style="white-space:pre"> </span>FinalHttp finalhttp = new FinalHttp(); <span style="white-space:pre"> </span>finalhttp.download(apkurl, Environment <span style="white-space:pre"> </span>.getExternalStorageDirectory().getAbsolutePath()+"/mobilesafe2.0.apk", <span style="white-space:pre"> </span>new AjaxCallBack<File>() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onFailure(Throwable t, int errorNo, <span style="white-space:pre"> </span>String strMsg) { <span style="white-space:pre"> </span>t.printStackTrace(); <span style="white-space:pre"> </span>Toast.makeText(getApplicationContext(), "下载失败", 1).show(); <span style="white-space:pre"> </span>super.onFailure(t, errorNo, strMsg); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onLoading(long count, long current) { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>super.onLoading(count, current); <span style="white-space:pre"> </span>tv_update_info.setVisibility(View.VISIBLE); <span style="white-space:pre"> </span>//当前下载百分比 <span style="white-space:pre"> </span>int progress = (int) (current * 100 / count); <span style="white-space:pre"> </span>tv_update_info.setText("下载进度:"+progress+"%"); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onSuccess(File t) { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>super.onSuccess(t); <span style="white-space:pre"> </span>installAPK(t); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * 安装APK <span style="white-space:pre"> </span> * @param t <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>private void installAPK(File t) { <span style="white-space:pre"> </span> Intent intent = new Intent(); <span style="white-space:pre"> </span> intent.setAction("android.intent.action.VIEW"); <span style="white-space:pre"> </span> intent.addCategory("android.intent.category.DEFAULT"); <span style="white-space:pre"> </span> intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive"); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> startActivity(intent); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>} else { <span style="white-space:pre"> </span>Toast.makeText(getApplicationContext(), "没有sdcard,请安装上在试", <span style="white-space:pre"> </span>0).show(); <span style="white-space:pre"> </span>return; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>builder.setNegativeButton("下次再说", new OnClickListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int which) { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>dialog.dismiss(); <span style="white-space:pre"> </span>enterHome();// 进入主页面 <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>builder.show(); <span style="white-space:pre"> </span>}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。