Android(Java)版本更新

Android開篇小記:入坑至今被安卓搞得焦頭爛額。接手外包人員一期工程開始硬著頭皮一邊學(xué)一遍寫。遇到了版本自動(dòng)更新失敗,無法完成彈出系統(tǒng)安裝檢測app界面。研究了兩天,總結(jié)一下我的經(jīng)驗(yàn),記錄下個(gè)人成長。最主要原因是將這些問題分享給此刻懵逼的你。
送你一句話:對(duì)于Android我是陌生的(寫了四年多的iOS,今年開始才接觸Android),我可以我想你比我還要強(qiáng)??,加油,相信自己!


注意事項(xiàng):我項(xiàng)目中判斷版本更新是在后臺(tái)完成的,當(dāng)有新版本時(shí)。后臺(tái)會(huì)給我返回code = 9999且會(huì)將版本信息返回給我。


資源文件傳送門

Android(Java)線上App版本更新步驟

  1. 創(chuàng)建更新DiaLog
  2. 在AndroidManifest.xml文件中加入權(quán)限
  3. 使用DownloadManager下載apk.->DownloadManager的使用和解析
  4. 創(chuàng)建通知監(jiān)聽APK是否下載完成。

1)、在AndroidManifest.xml文件中加入權(quán)限
2)、創(chuàng)建InstallReceiver.java

一、創(chuàng)建更新DiaLog

文件都可以在通過上面?zhèn)魉烷T找到。

效果圖
一、在AndroidManifest.xml文件中加入權(quán)限
 //Android8.0需要安裝權(quán)限
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
三、使用DownloadManager下載apk
public static void downloadFile(Context context, String name, String url) {
        Uri uri = Uri.parse(url);
        DownloadManager downloadManager = (DownloadManager) context
                .getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        //在通知欄中顯示
        request.setVisibleInDownloadsUi(true);
        request.setTitle("應(yīng)用更新");
        request.setDescription("");
        //MIME_MapTable是所有文件的后綴名所對(duì)應(yīng)的MIME類型的一個(gè)String數(shù)組  {".apk",    "application/vnd.android.package-archive"},
        request.setMimeType("application/vnd.android.package-archive");
        //指定下載地址
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name);
        //根據(jù)下載地址找到原來的安裝包并刪除
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        Uri uriFile =  Uri.withAppendedPath(Uri.fromFile(file), name);
        InstallReceiver.uriToFile(uriFile,context).delete();
        //下載管理Id
        downloadManager.enqueue(request);

    }
四、創(chuàng)建通知監(jiān)聽APK是否下載完成。

1)、在AndroidManifest.xml文件中加入權(quán)限

       <receiver android:name=".receivers.InstallReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            </intent-filter>
        </receiver>

2)、創(chuàng)建InstallReceiver.java
-- 以下為主要代碼,對(duì)應(yīng)文件請(qǐng)傳送!

//下載完成后響應(yīng)
      if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {

            //在廣播中取出下載任務(wù)的id
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            Toast.makeText(context, "編號(hào):" + id + "的下載任務(wù)已經(jīng)完成!", Toast.LENGTH_SHORT).show();
            DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadFileUri = dm.getUriForDownloadedFile(id);

            installApk(context, downloadFileUri);
        }
// 安裝Apk
    private void installApk(Context context, Uri filePath) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        File file = uriToFile(filePath,context);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//            Uri contentUri = FileProvider.getUriForFile(context, "${applicationId}.dataProvider", file);
            i.setDataAndType(filePath, "application/vnd.android.package-archive");
        } else {
            i.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
        }
        context.startActivity(i);
    }
結(jié)語:
   代碼在于折騰
   只要思想不滑坡,困難總比代碼多
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容