本篇文章給大家分享的是有關(guān)Android項(xiàng)目中怎么實(shí)現(xiàn)一個(gè)檢查更新、下載、安裝功能,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)企業(yè)建站,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),專注于網(wǎng)站建設(shè)技術(shù),精于網(wǎng)頁(yè)設(shè)計(jì),有多年建站和網(wǎng)站代運(yùn)營(yíng)經(jīng)驗(yàn),設(shè)計(jì)師為客戶打造網(wǎng)絡(luò)企業(yè)風(fēng)格,提供周到的建站售前咨詢和貼心的售后服務(wù)。對(duì)于成都做網(wǎng)站、成都網(wǎng)站制作中不同領(lǐng)域進(jìn)行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設(shè)中充分了解客戶行業(yè)的需求,以靈動(dòng)的思維在網(wǎng)頁(yè)中充分展現(xiàn),通過(guò)對(duì)客戶行業(yè)精準(zhǔn)市場(chǎng)調(diào)研,為客戶提供的解決方案。
android檢查更新、下載、安裝
1.MainActivity.Java:
public class MainActivity extends BaseActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckUpdateUtil.checkUpdate(this);//檢查更新 } }
2.CheckUpdateUtil.java:
package com.monkey.monkeymushroom.util; import android.app.AlertDialog; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.umeng.analytics.MobclickAgent; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 檢查更新工具類 */ public class CheckUpdateUtil { private static NotificationCompat.Builder builder; private static NotificationManager manager; private static final int UPDATE_ID = "0"; /** * 檢查更新 * * @param context * @return */ public static boolean checkUpdate(Context context) { // 獲取友盟在線參數(shù)(要更新的版本號(hào)) String force_version = MobclickAgent.getConfigParams(context, "version"); if (StringUtils.isEmpty(version)) { version = "1.0"; } // 版本號(hào)轉(zhuǎn)換為數(shù)組 String[] mUpdateVersionArray = version.split(","); String curr_version_name = SysInfoUtils.getVersionName(context); for (int i = 0; i < mUpdateVersionArray .length; i++) {//循環(huán)獲取在線參數(shù)上設(shè)置的版本號(hào) if (curr_version_name.equals(mUpdateVersionArray [i])) {//如果有,代表要更新 if ((mUpdateVersionArray .length > i + 1) && ("Y".equals(mUpdateVersionArray [i + 1]))) {//判斷是否強(qiáng)更 showUpdateDialog(true, context); } else {//不強(qiáng)更 showUpdateDialog(false, context); } return true;// 只要找到對(duì)應(yīng)的版本號(hào),即有更新,結(jié)束循環(huán) } } return false;//無(wú)更新 } /** * 顯示更新對(duì)話框 * * @param isForceUpdate 是否強(qiáng)制更新 */ public static void showUpdateDialog(final boolean isForceUpdate, final Context context) { // 獲取更新日志 String update_log = MobclickAgent.getConfigParams(context, "update_log"); // 最新版本 String new_version = MobclickAgent.getConfigParams(context, "new_version"); // 獲取下載地址 final String download_path = MobclickAgent.getConfigParams(context, "new_version_path"); if (TextUtils.isEmpty(update_log) || TextUtils.isEmpty(download_path) || TextUtils.isEmpty(new_version)) { return; } LogMessage.e("monkey", "更新日志--> " + update_log + " 最新版本--> " + new_version + " 下載地址--> " + download_path); //彈框提示 final AlertDialog mAlertDialog = new AlertDialog.Builder(context).create(); mAlertDialog.show(); mAlertDialog.setCancelable(false); Window window = mAlertDialog.getWindow(); window.setGravity(Gravity.BOTTOM); window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); View view = View.inflate(context, R.layout.dialog_update, null); window.setContentView(view); TextView log_head = (TextView) view.findViewById(R.id.log_head); TextView msg_tv = (TextView) view.findViewById(R.id.msg_tv); log_head.setText("v" + new_version + "更新日志:"); msg_tv.setText(update_log); Button update = (Button) view.findViewById(R.id.yes_btn); Button notNow = (Button) view.findViewById(R.id.no_btn); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { download(context, download_path); mAlertDialog.dismiss(); } }); notNow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAlertDialog.dismiss(); } }); if (isForceUpdate) {//如果是強(qiáng)制更新,則不顯示“以后再說(shuō)”按鈕 notNow.setVisibility(View.GONE); } } /** * 下載apk */ private static void download(final Context context, String download_path) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { String apkName = download_path.substring(download_path.lastIndexOf("/") + 1); String target = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + apkName; LogMessage.e("monkey", "apk target -->" + target); if (NetUtils.isNetConnect(context)) { HttpUtils httpUtils = new HttpUtils(1000 * 10);//為了方便使用了xUtils httpUtils.download(download_path, target, false, true, new RequestCallBack() { @Override public void onStart() { super.onStart(); ToastUtil.show(context, "正在下載……"); //創(chuàng)建通知欄下載提示 builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.ic_launcher) .setOngoing(true) .setContentTitle("猴菇先生"); manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); } @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); LogMessage.e("monkey", "--> total " + total + " current " + current); int cur = (int) (current * 100 / total); LogMessage.e("monkey", "cur--> " + cur + "%"); builder.setProgress(100, cur, false)//更新進(jìn)度 .setContentText(cur + "%"); manager.notify(UPDATE_ID, builder.build()); } @Override public void onSuccess(ResponseInfo responseInfo) { manager.cancel(UPDATE_ID);//取消通知欄下載提示 //下載成功后自動(dòng)安裝apk并打開(kāi) File file = responseInfo.result; Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } @Override public void onFailure(HttpException e, String s) { ToastUtil.show(context, "當(dāng)前網(wǎng)絡(luò)不可用,請(qǐng)檢查網(wǎng)絡(luò)設(shè)置"); } }); } else { ToastUtil.show(context, "當(dāng)前網(wǎng)絡(luò)不可用,請(qǐng)檢查網(wǎng)絡(luò)設(shè)置"); } } else { ToastUtil.show(context, "SD卡沒(méi)有插好"); } } }
3.更新彈框布局文件dialog_update.xml:
<?xml version="1.0" encoding="utf-8"?>
更新彈框:
以上就是Android項(xiàng)目中怎么實(shí)現(xiàn)一個(gè)檢查更新、下載、安裝功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。