Android開發,如何下載更新apk

思路是:獲取服務器最新版本,和手機當前版本比較,大於當前版本就下載更新,否則就什麼都不做

 

具體思路請參看我另一篇博客:https://blog.csdn.net/yijiaodingqiankun/article/details/83271042

 
//獲取手機中APP當前版本
public int getVersion() {
    try {
        //獲取包管理器
        PackageManager packageManager = getPackageManager();
        //顯示安裝包信息
        PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        //獲取版本名稱
        String versionName = packageInfo.versionName;
        //獲取版本號
        int versionCode = packageInfo.versionCode;

        return versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return 0;
}
  

 //下載apk的方法
    private void downloadApk(String versionUrl) {
//        (1)通過getSystemService獲取DownLoadManager。
        DownloadManager systemService = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//(     2)初始化DownLoadManager的Request,構建下載請求
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(BASE_URL + versionUrl));
//        Log.e("downloadApk: ",BASE_URL +versionUrl );
        //設置文件保存路徑
        request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "app-release.apk");

//(3)調用DownLoadManager的enqueue異步發起請求,該方法返回值爲標識當前下載任務的id,即downloadId
        long downloadId = systemService.enqueue(request);
//        4)當下載完成後,系統會發出條件爲android.intent.action.DOWNLOAD_COMPLETE的廣播,我們可以自定義廣播接受器,然後在onReceive中處理下載完成的邏輯即可。


    }

 


/**
 * Created by DELL zhanghuirong on 2020/1/26.
 */

//apk安裝接收器
public class ApkInstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            //安裝apk
            installApk(context, downloadId);
        }
    }


    /**
     * 安裝APK
     */
    private void installApk(Context context, long downloadId) {
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
        Uri uriForDownloadedFile = downloadManager.getUriForDownloadedFile(downloadId);
        if (uriForDownloadedFile != null) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(uriForDownloadedFile, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }else {
            ToastUtils.makeText("下載失敗");
        }
    }
//
//    private void removeFile() {
//        String filePath =
//        if (filePath != null) {
//            File downloadfile = new File(filePath);
//            if (downloadfile != null && downloadfile.exists()) {
////            刪除之前先判斷用戶是否已經安裝,安裝了才能刪除
//                if (hasSDK()) {
//                    downloadfile.delete();
//                    Log.e("removeFile: ","已刪除" );
//                }
//            }
//        }
//    }
}

 

在清單文件添加如下代碼

<!--安裝apk的廣播接收器-->
<receiver android:name=".bean.ApkInstallReceiver">
    <intent-filter>
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
    </intent-filter>
</receiver>
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章