Android 自動檢測版本升級
在我們APP的開發中,往往都會遇到版本的升級,因為不可能有任何一個應用做的完美無缺,所以版本升級對APP應用來說是不可缺少的一部分.像新浪微博等一些應用軟件,三天兩頭提醒我升級.不過這樣也很正常,就像android 升級一樣,為了給用戶提供更方便更人性化的操作.說下具體實現吧,不過我是參考別人的。不管對你們有沒有幫助,總之對我有幫助啊,如果日后用到就直接copy了.哈哈,不扯了。
首先看一個文件manifest文件.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jj.upgrade" android:versionCode="1" android:versionName="1.0" >我們可以很清楚的看到versionCode和versionName,我們一般用 versionCode來實現,
實現原理很簡單:服務器端有個serverVersion,我們本地有個localVersion.服務器端serverVersion>localVersion,這個時候我們就需要進行升級版本.原理大致就是這樣。具體實現請看下面.
package com.jj.upgrade;import com.jj.Service.UpdateService;
import android.app.AlertDialog; import android.app.Application; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException;
/***
- MyApplication
- @author zhangjia
*/ public class MyApplication extends Application {
public static int localVersion = 0;// 本地安裝版本
public static int serverVersion = 2;// 服務器版本
public static String downloadDir = "jj/";// 安裝目錄
@Override public void onCreate() {
super.onCreate(); try { PackageInfo packageInfo = getApplicationContext() .getPackageManager().getPackageInfo(getPackageName(), 0); localVersion = packageInfo.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } /*** * 在這里寫一個方法用于請求獲取服務器端的serverVersion. */
}
}</pre>
我們一般把全局的東西放到application里面.
public class MainActivity extends Activity { private MyApplication myApplication;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); checkVersion(); } /*** * 檢查是否更新版本 */ public void checkVersion() { myApplication = (MyApplication) getApplication(); if (myApplication.localVersion < myApplication.serverVersion) { // 發現新版本,提示用戶更新 AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("軟件升級") .setMessage("發現新版本,建議立即更新使用.") .setPositiveButton("更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent updateIntent = new Intent( MainActivity.this, UpdateService.class); updateIntent.putExtra( "app_name", getResources().getString( R.string.app_name)); startService(updateIntent); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.create().show(); } }
}</pre>我們在運行應用的時候要checkVersion();進行檢查版本是否要進行升級.
最主要的是UpdateService服務類,
@Override public int onStartCommand(Intent intent, int flags, int startId) {app_name = intent.getStringExtra("app_name"); // 創建文件 FileUtil.createFile(app_name);// 創建文件 createNotification();// 首次創建 createThread();// 線程下載 return super.onStartCommand(intent, flags, startId); }</pre> <p></p>
創建路徑及文件,這里就不介紹了,不明白了下載源碼看.
首先我們先 看createNotification().這個方法:
/**** 創建通知欄 */ RemoteViews contentView; public void createNotification() { notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notification = new Notification(); notification.icon = R.drawable.ic_launcher;// 這個圖標必須要設置,不然下面那個RemoteViews不起作用. // 這個參數是通知提示閃出來的值. notification.tickerText = "開始下載"; // // updateIntent = new Intent(this, MainActivity.class); // pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0); // // // 這里面的參數是通知欄view顯示的內容 // notification.setLatestEventInfo(this, app_name, "下載:0%", // pendingIntent); // // notificationManager.notify(notification_id, notification); /*** * 在這里我們用自定的view來顯示Notification */ contentView = new RemoteViews(getPackageName(), R.layout.notification_item); contentView.setTextViewText(R.id.notificationTitle, "正在下載"); contentView.setTextViewText(R.id.notificationPercent, "0%"); contentView.setProgressBar(R.id.notificationProgress, 100, 0, false); notification.contentView = contentView; updateIntent = new Intent(this, MainActivity.class); updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0); notification.contentIntent = pendingIntent; notificationManager.notify(notification_id, notification); }</pre>上面實現的也不難理解.(主要是初始化Notification,用于提醒用戶開始下載) <p></p>
接著我們要看createThread方法
/**** 開線程下載 */ public void createThread() { /*** * 更新UI */ final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case DOWN_OK: // 下載完成,點擊安裝 Uri uri = Uri.fromFile(FileUtil.updateFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.android.package-archive"); pendingIntent = PendingIntent.getActivity( UpdateService.this, 0, intent, 0); notification.setLatestEventInfo(UpdateService.this, app_name, "下載成功,點擊安裝", pendingIntent); notificationManager.notify(notification_id, notification); stopSelf(); break; case DOWN_ERROR: notification.setLatestEventInfo(UpdateService.this, app_name, "下載失敗", pendingIntent); break; default: stopSelf(); break; } } }; final Message message = new Message(); new Thread(new Runnable() { @Override public void run() { try { long downloadSize = downloadUpdateFile(down_url, FileUtil.updateFile.toString()); if (downloadSize > 0) { // 下載成功 message.what = DOWN_OK; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); message.what = DOWN_ERROR; handler.sendMessage(message); } } }).start(); }</pre>這個方法有點小多,不過我想大家都看的明白,我在這里簡單說名一下:首先我們創建一個handler用于檢測最后下載ok還是not ok. <p></p>
下面我們開啟了線程進行下載數據。
我們接著看downloadUpdateFile這個方法:
/**** 下載文件 * * @return * @throws MalformedURLException */ public long downloadUpdateFile(String down_url, String file) throws Exception { int down_step = 5;// 提示step int totalSize;// 文件總大小 int downloadCount = 0;// 已經下載好的大小 int updateCount = 0;// 已經上傳的文件大小 InputStream inputStream; OutputStream outputStream; URL url = new URL(down_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(TIMEOUT); httpURLConnection.setReadTimeout(TIMEOUT); // 獲取下載文件的size totalSize = httpURLConnection.getContentLength(); if (httpURLConnection.getResponseCode() == 404) { throw new Exception("fail!"); } inputStream = httpURLConnection.getInputStream(); outputStream = new FileOutputStream(file, false);// 文件存在則覆蓋掉 byte buffer[] = new byte[1024]; int readsize = 0; while ((readsize = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, readsize); downloadCount += readsize;// 時時獲取下載到的大小 /** * 每次增張5% */ if (updateCount == 0 || (downloadCount * 100 / totalSize - down_step) >= updateCount) { updateCount += down_step; // 改變通知欄 // notification.setLatestEventInfo(this, "正在下載...", updateCount // + "%" + "", pendingIntent); contentView.setTextViewText(R.id.notificationPercent, updateCount + "%"); contentView.setProgressBar(R.id.notificationProgress, 100, updateCount, false); // show_view notificationManager.notify(notification_id, notification); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } inputStream.close(); outputStream.close(); return downloadCount; }</pre>注釋已經寫的很詳細,相信大家都看的明白,如果哪里有不足的地方,請留您吉言指出. <p></p>
這里我用別的app代替了,簡單省事,正常的話,你要對你的APP進行數字簽名.然后才可以進行升級應用.
示意圖:
![]()
![]()
![]()
轉自:http://blog.csdn.net/jj120522/article/details/7948554