android的apk自動檢測升級,androidapk檢測

來源:互聯網
上載者:User

android的apk自動檢測升級,androidapk檢測

首先擷取本地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_namecreateFile(ctx.getResources().getString(R.string.app_name));new AsyncTask<String, Void, String>() {@Overrideprotected String doInBackground(String... params) {int down_step = 1;// 提示stepint 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);// 擷取下載檔案的sizetotalSize = 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;}@Overrideprotected 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後再安裝







安卓資源套件(apk)可以自動升級?

樓主不妨去下載一個豌豆莢。串連資料線後會自動識別你的手機型號。同時可以幫你更新手機裡的軟體。還有成千上萬的軟體等東西。很方便。望採納。
 
android更新完軟體後的APK在哪,就是說他舊的APK會自動覆蓋或刪除

是這樣的。
除非你用安卓市場類軟體下載,並且設定儲存源檔案,就丟不了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.