Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android的apk自動檢測升級

android的apk自動檢測升級

編輯:關於Android編程

首先獲取本地apk版本:

/**
* 獲取本地軟件版本
*/
public static int getLocalVersion(Context ctx){
int localVersion = 0;
try {
PackageInfo packageInfo = ctx.getApplicationContext()
.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
localVersion = packageInfo.versionCode;
Log.d("TAG", "本軟件的版本。。"+localVersion);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return localVersion;
}


然後獲取服務器版本,這個可以通過很多方式獲取就自己發揮

獲取服務器版本後跟本地apk版本進行比較:

如果低於服務器版本就進行更新:(本人寫的僅供參考,我是異步下載的,然後通知的方式顯示進度)

/**
	 * 用於更新app版本
	 * 
	 * @param ctx
	 *            上下文對象
	 * @param url
	 *            更新版本的地址
	 */
	private static PendingIntent pendingIntent;

	public static void UpdateVersion(final Context ctx, String url) {
		createNotification(ctx);
		// 創建文件,讀取app_name
		createFile(ctx.getResources().getString(R.string.app_name));
		new AsyncTask() {
			@Override
			protected String doInBackground(String... params) {
				int down_step = 1;// 提示step
				int totalSize;// 文件總大小
				int downloadCount = 0;// 已經下載好的大小
				int updateCount = 0;// 已經上傳的文件大小
				InputStream inputStream;
				OutputStream outputStream;

				URL url;
				HttpURLConnection httpURLConnection = null;
				try {
					url = new URL(params[0]);
					httpURLConnection = (HttpURLConnection) url
							.openConnection();
					httpURLConnection.setConnectTimeout(TIMEOUT);
					httpURLConnection.setReadTimeout(TIMEOUT);
					// 獲取下載文件的size
					totalSize = httpURLConnection.getContentLength();
					// Log.d("TAG", "totalSize"+totalSize);
					if (httpURLConnection.getResponseCode() == 404) {
						throw new Exception("fail!");
					}
					inputStream = httpURLConnection.getInputStream();
					// File appFile=File.createTempFile("中拓鋼鐵",".apk");
					outputStream = new FileOutputStream(updateFile, false);// 文件存在則覆蓋掉
					byte buffer[] = new byte[1024];
					int readsize = 0;
					while ((readsize = inputStream.read(buffer)) != -1) {
						outputStream.write(buffer, 0, readsize);
						downloadCount += readsize;// 時時獲取下載到的大小
						// Log.d("TAG", "downloadCount"+downloadCount);
						/**
						 * 每次增張1%
						 */
						if (updateCount == 0
								|| (downloadCount * 100 / totalSize - down_step) >= updateCount) {
							updateCount += down_step;
							// 改變通知欄
							// Log.d("TAG", "開始下載。。");
							builder = new Notification.Builder(ctx)
									.setSmallIcon(R.drawable.logo)
									.setContentText(
											"正在下載(" + updateCount + "%)...")
									.setProgress(100, updateCount, false);
							manager.notify(8, builder.build());
						}

					}
					if (httpURLConnection != null) {
						httpURLConnection.disconnect();
					}
					inputStream.close();
					outputStream.close();
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}

				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				// 下載完成,點擊安裝
				System.out.println("updateFile.." + updateFile);
				Uri uri = Uri.fromFile(updateFile);
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(uri,
						"application/vnd.android.package-archive");

				pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
				builder = new Notification.Builder(ctx)
						.setSmallIcon(R.drawable.logo)
						.setContentIntent(pendingIntent)
						.setContentTitle("apk下載更新")
						.setContentText("下載成功,請點擊安裝")
						.setProgress(100, 100, false);
				manager.notify(8, builder.build());

			}
		}.execute(url);

	}

	private static void createNotification(Context ctx) {

		// 最普通的通知欄
		manager = (NotificationManager) ctx
				.getSystemService(ctx.NOTIFICATION_SERVICE);
		builder = new Notification.Builder(ctx).setSmallIcon(R.drawable.logo)
				.setContentTitle("apk下載更新").setContentText("准備下載...")
				.setProgress(100, 0, false);
		manager.notify(8, builder.build());
	}

當下載完成後,可以點擊安裝。

注:當你的apk是簽名後的,如果手機上面的apk是沒有簽名的,那麼在安裝時會提示簽名不同的包沖突,無法安裝,這樣只能卸載沒有簽名的apk後再安裝






  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved