android應(yīng)用更新升級(jí)模塊升級(jí)會(huì)卡引發(fā)CF-創(chuàng)新互聯(lián)

    今天在做應(yīng)用升級(jí)的模塊的時(shí)候,給大家分享一下升級(jí)的相關(guān)代碼模塊,在升級(jí)的過程前期的下載,和現(xiàn)在彈出的相關(guān)窗體這些的沒有什么難度的,就是有個(gè)重要的地方,在每次要刷新下載進(jìn)度更新的時(shí)候,要給個(gè)有條件更新,如果每次夠讓他跟新的,會(huì)導(dǎo)致應(yīng)用邊卡,引發(fā)CF等問題,下面是相關(guān)的代碼分享,若其他問題可以私聊我?。?!

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比修水網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式修水網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋修水地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

     本應(yīng)用用到了兩個(gè)開源庫:

    需要的人找我私聊

public class MainActivity extends Activity {

 private int mCurVersion;
 private ObjUpData updata;
 private static NotificationManager mNm;
 private static RemoteViews mrRemoteViews;
 private static Notification notification;
 private static int tmp;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mNm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//  點(diǎn)擊下載
  findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    updatautil();
   }
  });
  //若點(diǎn)擊下載后,在點(diǎn)別的會(huì)倒是CF
  findViewById(R.id.button2).setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "卡了?。∥揖秃呛橇?, Toast.LENGTH_SHORT).show();

   }
  });
 }
 public int getCurrentVersion() {
  PackageInfo info = null;
  try {
   PackageManager pm = getPackageManager();
   info = pm.getPackageInfo(getPackageName(), 0);
  } catch (NameNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Log.e("getCurrentVersion", "info.versionCode=" + info.versionCode);
  return info.versionCode;
 }
 private void updatautil() {
  // 小模塊:實(shí)現(xiàn)版本升級(jí)
  // 打開應(yīng)用
  // ---->有新版本,彈出dialog(更新了xx功能,修復(fù)了xxbug,提升自動(dòng)定位的準(zhǔn)確性...);
  // 如果點(diǎn)擊取消就不更新,
  // 點(diǎn)擊確定才開始下載最新安裝包
  // --->下載完成,下拉點(diǎn)擊,跳轉(zhuǎn)到安裝界面
  // 有新版本,彈出dialog
  // 獲取當(dāng)前版本
      mCurVersion = getCurrentVersion();
  // 獲取新版本,下載文件,解析得到文件中所有數(shù)據(jù)

  HttpUtil.post("http://192.168.1.103:8080/tins//equRgController/doDownloadNewVersion.do",//下載路徑
    new RequestParams(), new AsyncHttpResponseHandler() {
   @Override

   public void onSuccess(int statusCode, Header[] headers, String content) {
    Log.e("HttpUtil.post", "statusCode=" + statusCode + "headers=" + headers + "content=" + content);
    UpData data = new Gson().fromJson(content, UpData.class);
    updata = data.getObj();
    // 得到新版本號(hào)
    int newVersion = updata.getVersion();
    // 將新版本號(hào)跟就版本進(jìn)行比較
    if (newVersion > mCurVersion) {
     // 彈出dialog提示是否需要更新并顯示更新簡介
     showMyDialog();
    }
   }

   @Override
   public void onFailure(int statusCode, Header[] headers, Throwable error, String content) {
   }

  });
 }

 private void showMyDialog() {
  final Dialog mDialog = new Dialog(this);
  mDialog.setTitle("發(fā)現(xiàn)新版本");
  View view = getLayoutInflater().inflate(R.layout.dialog_item, null);
  TextView textView = (TextView) view.findViewById(R.id.textView1);
  textView.setText(updata.getDesc());
  view.findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    mDialog.dismiss();
    new MyAsyn(mNm).execute(updata.getLoadUrl());
    ShowPedding();
   }

  });
  view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    mDialog.dismiss();
   }

  });
  mDialog.setContentView(view);
  mDialog.show();

 }

//啟動(dòng)異步下載

 static class MyAsyn extends AsyncTask<String, Integer, Integer> {

  private NotificationManager mNm;

  public MyAsyn(NotificationManager mNm) {
   this.mNm = mNm;
  }

  @Override
  protected Integer doInBackground(String... params) {
   int length = 0;
   FileOutputStream fos = null;
   try {
    URL url = new URL(params[0]);
    URLConnection openConnection = url.openConnection();
    InputStream is = openConnection.getInputStream();
    length = openConnection.getContentLength();
    byte[] buffer = new byte[1024];
    int end = 0;
    int sum = 0;
    Log.e("doInBackground", "Environment.getExternalStorageDirectory().getPath()="
      + Environment.getExternalStorageDirectory().getPath());
    fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/TTins.apk");
    // fos = new FileOutputStream("/mnt/sdcard/TTins.apk");
    while (-1 != (end = is.read(buffer))) {
     fos.write(buffer, 0, end);
     int resent = sum * 100 / length;
     sum += end;
     if (resent % 6 == 0&&tmp!=resent) {
      tmp=resent;
      publishProgress(length, sum, resent);
     }
    }
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    if (fos != null) {
     try {
      fos.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
   return length;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   mrRemoteViews.setProgressBar(R.id.progressBar1, values[0], values[1], false);
   mrRemoteViews.setTextViewText(R.id.textView1, "已下載" + values[2] + "%");
   mNm.notify(1, notification);
   super.onProgressUpdate(values);
  }

  @Override
  protected void onPostExecute(Integer result) {
   mrRemoteViews.setProgressBar(R.id.progressBar1, result, result, false);
   mrRemoteViews.setTextViewText(R.id.textView1, "下載完成");
   mNm.notify(1, notification);
   super.onPostExecute(result);
  }

 }

 private void ShowPedding() {
  // private static void ShowPedding(Activity activity,
  // NotificationManager mNm) {
  String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TTins.apk";
  // 創(chuàng)建URI
  Uri uri = Uri.fromFile(new File(fileName));
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(uri, "application/vnd.android.package-archive");
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  mrRemoteViews = new RemoteViews(getPackageName(), R.layout.remote_item);
  notification = new NotificationCompat.Builder(this).setTicker("開始下載").setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pendingIntent).setContent(mrRemoteViews).build();
  mNm.notify(1, (Notification) notification);
 }

}

彈出dialog的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:background="#ffffff"
       android:layout_alignParentTop="true" >

       <ImageView
           android:id="@+id/p_w_picpathView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentTop="true"
           android:layout_centerHorizontal="true"
           android:src="@drawable/ic_launcher" />

       <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_below="@+id/p_w_picpathView1"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="22dp"
           android:text="Large Text"
           android:textAppearance="?android:attr/textAppearanceLarge" />

       <Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_below="@+id/textView1"
           android:layout_marginTop="23dp"
           android:layout_toLeftOf="@+id/p_w_picpathView1"
           android:text="取消" />

       <Button
           android:id="@+id/button2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/button1"
           android:layout_alignBottom="@+id/button1"
           android:layout_toRightOf="@+id/p_w_picpathView1"
           android:text="確定" />
   </RelativeLayout>

</RelativeLayout>

下面分享一下要用的兩個(gè)XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <ProgressBar
       android:id="@+id/progressBar1"

       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/textView1"
       android:layout_toLeftOf="@+id/button1"
       android:layout_toRightOf="@+id/p_w_picpathView1" />

   <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       android:text="Button" />

   <ImageView
       android:id="@+id/p_w_picpathView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:src="@drawable/ic_launcher" />

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_toRightOf="@+id/p_w_picpathView1"
       android:text="Large Text"
       android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:android應(yīng)用更新升級(jí)模塊升級(jí)會(huì)卡引發(fā)CF-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://muchs.cn/article26/cospcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站制作、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)