A-A+

android 应用更新升级模块 升级会卡 引发CF

2016年02月17日 站长资讯 暂无评论

今天在做应用升级的模块的时候,给大家分享一下升级的相关代码模块,在升级的过程前期的下载,和现在弹出的相关窗体这些的没有什么难度的,就是有个重要的地方,在每次要刷新下载进度更新的时候,要给个有条件更新,如果每次够让他跟新的,会导致应用边卡,引发CF等问题,下面是相关的代码分享,若其他问题可以私聊我!!!

本应用用到了两个开源库:

需要的人找我私聊.

  1. public class MainActivity extends Activity {  
  2.  private int mCurVersion;  
  3.  private ObjUpData updata;  
  4.  private static NotificationManager mNm;  
  5.  private static RemoteViews mrRemoteViews;  
  6.  private static Notification notification;  
  7.  private static int tmp;  
  8.  @Override  
  9.  protected void onCreate(Bundle savedInstanceState) {  
  10.   super.onCreate(savedInstanceState);  
  11.   setContentView(R.layout.activity_main);  
  12.   mNm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  13. //  点击下载  
  14.   findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
  15.      
  16.    @Override  
  17.    public void onClick(View v) {  
  18.     updatautil();  
  19.    }  
  20.   });  
  21.   //若点击下载后,在点别的会倒是CF  
  22.   findViewById(R.id.button2).setOnClickListener(new OnClickListener() {  
  23.      
  24.    @Override  
  25.    public void onClick(View v) {  
  26.     // TODO Auto-generated method stub  
  27.     Toast.makeText(MainActivity.this, "卡了!!我就呵呵了", Toast.LENGTH_SHORT).show();  
  28.       
  29.    }  
  30.   });  
  31.  }  
  32.  public int getCurrentVersion() {  
  33.   PackageInfo info = null;  
  34.   try {  
  35.    PackageManager pm = getPackageManager();  
  36.    info = pm.getPackageInfo(getPackageName(), 0);  
  37.   } catch (NameNotFoundException e) {  
  38.    // TODO Auto-generated catch block  
  39.    e.printStackTrace();  
  40.   }  
  41.   Log.e("getCurrentVersion", "info.versionCode=" + info.versionCode);  
  42.   return info.versionCode;  
  43.  }  
  44.  private void updatautil() {  
  45.   // 小模块:实现版本升级  
  46.   // 打开应用  
  47.   // ---->有新版本,弹出dialog(更新了xx功能,修复了xxbug,提升自动定位的准确性...);  
  48.   // 如果点击取消就不更新,  
  49.   // 点击确定才开始下载最新安装包  
  50.   // --->下载完成,下拉点击,跳转到安装界面  
  51.   // 有新版本,弹出dialog  
  52.   // 获取当前版本  
  53.       mCurVersion = getCurrentVersion();  
  54.   // 获取新版本,下载文件,解析得到文件中所有数据  
  55.     
  56.   HttpUtil.post("http://192.168.1.103:8080/tins//equRgController/doDownloadNewVersion.do",//下载路径  
  57.     new RequestParams(), new AsyncHttpResponseHandler() {  
  58.    @Override  
  59.      
  60.    public void onSuccess(int statusCode, Header[] headers, String content) {  
  61.     Log.e("HttpUtil.post", "statusCode=" + statusCode + "headers=" + headers + "content=" + content);  
  62.     UpData data = new Gson().fromJson(content, UpData.class);  
  63.     updata = data.getObj();  
  64.     // 得到新版本号  
  65.     int newVersion = updata.getVersion();  
  66.     // 将新版本号跟就版本进行比较  
  67.     if (newVersion > mCurVersion) {  
  68.      // 弹出dialog提示是否需要更新并显示更新简介  
  69.      showMyDialog();  
  70.     }  
  71.    }  
  72.    @Override  
  73.    public void onFailure(int statusCode, Header[] headers, Throwable error, String content) {  
  74.    }  
  75.   });  
  76.  }  
  77.  private void showMyDialog() {  
  78.   final Dialog mDialog = new Dialog(this);  
  79.   mDialog.setTitle("发现新版本");  
  80.   View view = getLayoutInflater().inflate(R.layout.dialog_item, null);  
  81.   TextView textView = (TextView) view.findViewById(R.id.textView1);  
  82.   textView.setText(updata.getDesc());  
  83.   view.findViewById(R.id.button2).setOnClickListener(new OnClickListener() {  
  84.    @Override  
  85.    public void onClick(View v) {  
  86.     mDialog.dismiss();  
  87.     new MyAsyn(mNm).execute(updata.getLoadUrl());  
  88.     ShowPedding();  
  89.    }  
  90.   });  
  91.   view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
  92.    @Override  
  93.    public void onClick(View v) {  
  94.     mDialog.dismiss();  
  95.    }  
  96.   });  
  97.   mDialog.setContentView(view);  
  98.   mDialog.show();  
  99.  }  
  100.     
  101. //启动异步下载  
  102.  static class MyAsyn extends AsyncTask<String, Integer, Integer> {  
  103.   private NotificationManager mNm;  
  104.   public MyAsyn(NotificationManager mNm) {  
  105.    this.mNm = mNm;  
  106.   }  
  107.   @Override  
  108.   protected Integer doInBackground(String... params) {  
  109.    int length = 0;  
  110.    FileOutputStream fos = null;  
  111.    try {  
  112.     URL url = new URL(params[0]);  
  113.     URLConnection openConnection = url.openConnection();  
  114.     InputStream is = openConnection.getInputStream();  
  115.     length = openConnection.getContentLength();  
  116.     byte[] buffer = new byte[1024];  
  117.     int end = 0;  
  118.     int sum = 0;  
  119.     Log.e("doInBackground", "Environment.getExternalStorageDirectory().getPath()="  
  120.       + Environment.getExternalStorageDirectory().getPath());  
  121.     fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/TTins.apk");  
  122.     // fos = new FileOutputStream("/mnt/sdcard/TTins.apk");  
  123.     while (-1 != (end = is.read(buffer))) {  
  124.      fos.write(buffer, 0, end);  
  125.      int resent = sum * 100 / length;  
  126.      sum += end;  
  127.      if (resent % 6 == 0&&tmp!=resent) {  
  128.       tmp=resent;  
  129.       publishProgress(length, sum, resent);  
  130.      }  
  131.     }  
  132.    } catch (IOException e) {  
  133.     e.printStackTrace();  
  134.    } finally {  
  135.     if (fos != null) {  
  136.      try {  
  137.       fos.close();  
  138.      } catch (IOException e) {  
  139.       e.printStackTrace();  
  140.      }  
  141.     }  
  142.    }  
  143.    return length;  
  144.   }  
  145.   @Override  
  146.   protected void onProgressUpdate(Integer... values) {  
  147.    mrRemoteViews.setProgressBar(R.id.progressBar1, values[0], values[1], false);  
  148.    mrRemoteViews.setTextViewText(R.id.textView1, "已下载" + values[2] + "%");  
  149.    mNm.notify(1, notification);  
  150.    super.onProgressUpdate(values);  
  151.   }  
  152.   @Override  
  153.   protected void onPostExecute(Integer result) {  
  154.    mrRemoteViews.setProgressBar(R.id.progressBar1, result, result, false);  
  155.    mrRemoteViews.setTextViewText(R.id.textView1, "下载完成");  
  156.    mNm.notify(1, notification);  
  157.    super.onPostExecute(result);  
  158.   }  
  159.  }  
  160.    
  161.  private void ShowPedding() {  
  162.   // private static void ShowPedding(Activity activity,  
  163.   // NotificationManager mNm) {  
  164.   String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TTins.apk";  
  165.   // 创建URI  
  166.   Uri uri = Uri.fromFile(new File(fileName));  
  167.   Intent intent = new Intent(Intent.ACTION_VIEW);  
  168.   intent.setDataAndType(uri, "application/vnd.android.package-archive");  
  169.   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);  
  170.   mrRemoteViews = new RemoteViews(getPackageName(), R.layout.remote_item);  
  171.   notification = new NotificationCompat.Builder(this).setTicker("开始下载").setSmallIcon(R.drawable.ic_launcher)  
  172.     .setContentIntent(pendingIntent).setContent(mrRemoteViews).build();  
  173.   mNm.notify(1, (Notification) notification);  
  174.  }  
  175.    
  176. }  

弹出dialog的XML文件.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.     <RelativeLayout  
  5.         android:layout_width="match_parent"  
  6.         android:layout_height="wrap_content"  
  7.         android:layout_alignParentLeft="true"  
  8.         android:background="#ffffff"  
  9.         android:layout_alignParentTop="true" >  
  10.          
  11.         <ImageView  
  12.             android:id="@+id/imageView1"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_alignParentTop="true"  
  16.             android:layout_centerHorizontal="true"  
  17.             android:src="@drawable/ic_launcher" />  
  18.         <TextView  
  19.             android:id="@+id/textView1"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_below="@+id/imageView1"  
  23.             android:layout_centerHorizontal="true"  
  24.             android:layout_marginTop="22dp"  
  25.             android:text="Large Text"  
  26.             android:textAppearance="?android:attr/textAppearanceLarge" />  
  27.         <Button  
  28.             android:id="@+id/button1"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content"  
  31.             android:layout_below="@+id/textView1"  
  32.             android:layout_marginTop="23dp"  
  33.             android:layout_toLeftOf="@+id/imageView1"  
  34.             android:text="取消" />  
  35.         <Button  
  36.             android:id="@+id/button2"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:layout_alignBaseline="@+id/button1"  
  40.             android:layout_alignBottom="@+id/button1"  
  41.             android:layout_toRightOf="@+id/imageView1"  
  42.             android:text="确定" />  
  43.     </RelativeLayout>  
  44. </RelativeLayout>  

下面分享一下要用的两个XML文件.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.     <ProgressBar  
  5.         android:id="@+id/progressBar1"  
  6.         style="?android:attr/progressBarStyleHorizontal"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_below="@+id/textView1"  
  10.         android:layout_toLeftOf="@+id/button1"  
  11.         android:layout_toRightOf="@+id/imageView1" />  
  12.     <Button  
  13.         android:id="@+id/button1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_alignParentRight="true"  
  17.         android:layout_alignParentTop="true"  
  18.         android:text="Button" />  
  19.     <ImageView  
  20.         android:id="@+id/imageView1"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_alignParentLeft="true"  
  24.         android:layout_alignParentTop="true"  
  25.         android:src="@drawable/ic_launcher" />  
  26.     <TextView  
  27.         android:id="@+id/textView1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_alignParentTop="true"  
  31.         android:layout_toRightOf="@+id/imageView1"  
  32.         android:text="Large Text"  
  33.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  34. </RelativeLayout>  
标签:

给我留言