最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android  檢查更新、下載、安裝功能的實現(xiàn)

 更新時間:2017年01月23日 10:44:45   投稿:lqh  
這篇文章主要介紹了Android 檢查更新、下載、安裝功能的實現(xiàn)的相關(guān)資料,這里附有實例代碼,具有一定的參考價值,需要的朋友可以參考下

android檢查更新、下載、安裝

前言:

由于友盟更新即將下線,我們就修改了更新邏輯,自己檢查更新、下載、安裝,但是檢查更新還是要依賴于友盟中的在線參數(shù):

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ù)(要更新的版本號)
  String force_version = MobclickAgent.getConfigParams(context, "version");
  if (StringUtils.isEmpty(version)) {
   version = "1.0";
  }
  // 版本號轉(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è)置的版本號
   if (curr_version_name.equals(mUpdateVersionArray [i])) {//如果有,代表要更新
    if ((mUpdateVersionArray .length > i + 1) && ("Y".equals(mUpdateVersionArray [i + 1]))) {//判斷是否強更
     showUpdateDialog(true, context);
    } else {//不強更
     showUpdateDialog(false, context);
    }
    return true;// 只要找到對應(yīng)的版本號,即有更新,結(jié)束循環(huán)
   }
  }
  return false;//無更新
 }

 /**
  * 顯示更新對話框
  *
  * @param isForceUpdate 是否強制更新
  */
 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) {//如果是強制更新,則不顯示“以后再說”按鈕
   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<File>() {
     @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)//更新進度
        .setContentText(cur + "%");
      manager.notify(UPDATE_ID, builder.build());
     }

     @Override
     public void onSuccess(ResponseInfo<File> responseInfo) {
      manager.cancel(UPDATE_ID);//取消通知欄下載提示
      //下載成功后自動安裝apk并打開
      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ò)不可用,請檢查網(wǎng)絡(luò)設(shè)置");
     }
    });
   } else {
    ToastUtil.show(context, "當(dāng)前網(wǎng)絡(luò)不可用,請檢查網(wǎng)絡(luò)設(shè)置");
   }
  } else {
   ToastUtil.show(context, "SD卡沒有插好");
  }
 }
}

3.更新彈框布局文件dialog_update.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <RelativeLayout
  android:layout_width="310dp"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:background="@color/base_white"
  android:paddingBottom="20dp">

  <TextView
   android:id="@+id/title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="20dp"
   android:layout_marginTop="20dp"
   android:text="發(fā)現(xiàn)新版本"
   android:textSize="20sp" />

  <View
   android:id="@+id/line"
   android:layout_width="match_parent"
   android:layout_height="1dp"
   android:layout_below="@+id/title"
   android:background="#4a7acd" />

  <TextView
   android:id="@+id/log_head"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/line"
   android:layout_marginLeft="20dp"
   android:layout_marginTop="20dp" />

  <TextView
   android:id="@+id/msg_tv"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/log_head"
   android:layout_centerHorizontal="true"
   android:layout_marginLeft="32dp"
   android:layout_marginRight="32dp"
   android:text="更新日志"
   android:textSize="16sp" />

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="35dp"
   android:layout_below="@+id/msg_tv"
   android:layout_marginTop="30dp"
   android:orientation="horizontal">

   <Button
    android:id="@+id/yes_btn"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_weight="1"
    android:background="#4a7acd"
    android:gravity="center"
    android:text="立即更新"
    android:textColor="@color/base_white"
    android:textSize="16sp" />

   <Button
    android:id="@+id/no_btn"
    android:layout_width="0dp"
    android:layout_height="35dp"
    android:layout_marginRight="20dp"
    android:layout_weight="1"
    android:background="#4a7acd"
    android:gravity="center"
    android:text="以后再說"
    android:textColor="@color/base_white"
    android:textSize="16sp" />
  </LinearLayout>
 </RelativeLayout>
</RelativeLayout>

更新彈框:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Android仿簡書動態(tài)searchview搜索欄效果

    Android仿簡書動態(tài)searchview搜索欄效果

    這篇文章主要為大家詳細介紹了Android仿簡書動態(tài)searchview效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android 歡迎全屏圖片詳解及實例代碼

    Android 歡迎全屏圖片詳解及實例代碼

    這篇文章主要介紹了Android 歡迎全屏圖片詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Flutter 狀態(tài)管理的實現(xiàn)

    Flutter 狀態(tài)管理的實現(xiàn)

    這篇文章主要介紹了Flutter 狀態(tài)管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Android 實現(xiàn)不同字體顏色的TextView實現(xiàn)代碼

    Android 實現(xiàn)不同字體顏色的TextView實現(xiàn)代碼

    這篇文章主要介紹了Android 實現(xiàn)不同字體顏色的TextView實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android中Activity啟動默認(rèn)不顯示輸入法解決方法

    Android中Activity啟動默認(rèn)不顯示輸入法解決方法

    這篇文章主要介紹了Android中Activity啟動默認(rèn)不顯示輸入法解決方法,一般是因為包含checkbox控件導(dǎo)致Activity啟動默認(rèn)不顯示輸入法,本文給出了正確解決方法,需要的朋友可以參考下
    2015-06-06
  • Android編程之代碼創(chuàng)建布局實例分析

    Android編程之代碼創(chuàng)建布局實例分析

    這篇文章主要介紹了Android編程之代碼創(chuàng)建布局的方法,結(jié)合實例形式分析了Android通過代碼創(chuàng)建布局的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android編程實現(xiàn)TextView垂直自動滾動功能【附demo源碼下載】

    Android編程實現(xiàn)TextView垂直自動滾動功能【附demo源碼下載】

    這篇文章主要介紹了Android編程實現(xiàn)TextView垂直自動滾動功能,詳細分析了Android TextView垂直自動滾動功能的實現(xiàn)步驟與布局、功能相關(guān)技巧,并附帶了demo源碼供讀者下載,需要的朋友可以參考下
    2017-02-02
  • Android+Flutter實現(xiàn)彩虹圖案的繪制

    Android+Flutter實現(xiàn)彩虹圖案的繪制

    彩虹,是氣象中的一種光學(xué)現(xiàn)象,當(dāng)太陽光照射到半空中的水滴,光線被折射及反射,在天空上形成拱形的七彩光譜。接下來,我們就自己手動繪制一下彩虹圖案吧
    2022-11-11
  • Android短信驗證服務(wù)分享

    Android短信驗證服務(wù)分享

    這篇文章主要為大家詳細介紹了Android短信驗證服務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Flutter Drawer抽屜菜單示例詳解

    Flutter Drawer抽屜菜單示例詳解

    這篇文章主要為大家詳細介紹了Flutter Drawer抽屜菜單示例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評論

聊城市| 当阳市| 连江县| 梁山县| 惠安县| 香河县| 永新县| 东光县| 苍山县| 太康县| 台南市| 沧源| 贵德县| 淄博市| 巴林左旗| 宁陕县| 绥棱县| 湖口县| 炎陵县| 淮北市| 乌拉特前旗| 太原市| 观塘区| 晋州市| 那曲县| 丹巴县| 永嘉县| 隆尧县| 峨山| 防城港市| 灵宝市| 东至县| 双峰县| 斗六市| 长春市| 永顺县| 绥江县| 连城县| 曲沃县| 荆门市| 宣恩县|