Android:使用 DownloadManager 進行版本更新


app 以前的版本更新使用的自己寫的代碼從服務器下載,結果出現了下載完成以后,提示解析包錯誤的問題,但是呢,找到該 apk 點擊安裝是可以安裝成功的,估計就是最后幾秒安裝包沒有下載完成然后點擊了安裝出現的解析包錯誤的問題。目前修改為通過 DownloadManager 進行下載。

代碼如下:

1. 判斷當前是否可以使用 DownloadManager (根據搜索結果,反饋說有些國產手機會把 DownloadManager 進行閹割掉,目前測試在 Nexus6, 華為Mate9, 小米 Note,華為Mate8, HTC D820U, 三星 S7 上可以使用)

 1     private static boolean canDownloadState(Context ctx) {
 2         try {
 3             int state = ctx.getPackageManager().getApplicationEnabledSetting("com.android.providers.downloads");
 4 
 5             if (state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
 6                     || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
 7                     || state == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
 8                 return false;
 9             }
10         } catch (Exception e) {
11             e.printStackTrace();
12             return false;
13         }
14         return true;
15     }

2. 出現可以使用和不可以使用的情況。可以使用的話,就使用 DownloadManager 進行下載,不可以使用就交給瀏覽器進行下載

 1                 if (canDownloadState(ctx)) {
 2                     MLog.d("UpdateVersion", "DownloadManager 可用");
 3                     Intent downloadApkIntent = new Intent(ctx, DownApkServer.class);
 4                     Bundle bundle = new Bundle();
 5                     bundle.putString("downloadUrl", url);
 6                     bundle.putString("title", subAppName);
 7                     downloadApkIntent.putExtra("download", bundle);
 8                     ctx.startService(downloadApkIntent);
 9                 } else {
10                     MLog.d("UpdateVersion", "DownloadManager 不可用");
11                     Intent intent = new Intent();
12                     intent.setAction("android.intent.action.VIEW");
13                     Uri content_url = Uri.parse(url);
14                     intent.setData(content_url);
15                     ctx.startActivity(intent);
16                 }

3. 啟動 DownApkServer 服務

 1 public class DownApkServer extends Service {
 2     static final String TAG = "DownApkServer";
 3     Context context = this;
 4     SharedPreferences mSp;
 5 
 6     public DownApkServer() {
 7 
 8     }
 9 
10     @Override
11     public IBinder onBind(Intent intent) {
12         return null;
13     }
14 
15     @Override
16     public int onStartCommand(Intent intent, int flags, int startId) {
17         Bundle downloadBundle = intent.getBundleExtra("download");
18         if (downloadBundle != null) {
19             String downloadUrl = downloadBundle.getString("downloadUrl");
20             String title = downloadBundle.getString("title");
21             if (!TextUtils.isEmpty(downloadUrl)) {
22                 mSp = context.getSharedPreferences("downloadApk", MODE_PRIVATE);
23                 long downloadId = downloadApk(downloadUrl, title);
24                 mSp.edit().putLong("downloadId", downloadId).commit();
25             }
26         }
27         stopSelf();
28         return super.onStartCommand(intent, flags, startId);
29     }
30 
31 
32     private long downloadApk(String url, String title) {
33         Uri downloadUri = Uri.parse(url);
34         DownloadManager.Request request = new DownloadManager.Request(downloadUri);
35         String apkName = title + ".apk";
36         File file = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/" + apkName);
37         if (file != null && file.exists()) {
38             file.delete();
39         }
40         request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,
41                 apkName);
42         mSp.edit().putString("apkName", apkName).commit();
43         request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
44         request.setVisibleInDownloadsUi(true);
45         request.setTitle(title);
46         DownloadManager mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
47         return mDownloadManager.enqueue(request);
48     }
49 }

4. 注冊一個廣播,DownApkReceiver

 1 public class DownApkReceiver extends BroadcastReceiver {
 2     private static final String TAG = "DownApkReceiver";
 3     SharedPreferences mSharedP;
 4     DownloadManager mManager;
 5     Context ctx;
 6 
 7     @Override
 8     public void onReceive(Context context, Intent intent) {
 9         ctx = context;
10         if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
11             long downloadApkId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
12             mSharedP = context.getSharedPreferences("downloadApk", MODE_PRIVATE);
13             long saveApkId = mSharedP.getLong("downloadId", -1L);
14             if (downloadApkId == saveApkId) {
15                 checkDownloadStatus(context, downloadApkId);
16             }
17         }
18     }
19 
20     private void checkDownloadStatus(Context context, long downloadId) {
21         mManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
22         DownloadManager.Query query = new DownloadManager.Query();
23         query.setFilterById(downloadId);
24         Cursor cursor = mManager.query(query);
25         if (cursor.moveToFirst()) {
26             int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
27             switch (status) {
28                 case DownloadManager.STATUS_SUCCESSFUL:
29                     installApk(context);
30                     break;
31                 case DownloadManager.STATUS_FAILED:
32                     MLog.d("DownApkReceiver", "下載失敗.....");
33                     break;
34                 case DownloadManager.STATUS_RUNNING:
35                     MLog.d("DownApkReceiver", "正在下載.....");
36                     break;
37                 default:
38                     break;
39             }
40         }
41     }
42 
43     private void installApk(Context context) {
44         String apkName = mSharedP.getString("apkName", null);
45         if (apkName != null) {
46             MLog.d("DownApkReceiver", "apkName 為" + apkName);
47             File file = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/" + apkName);
48             if (file != null) {
49                 Intent install = new Intent("android.intent.action.VIEW");
50                 Uri downloadFileUri = Uri.fromFile(file);
51                 install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
52                 install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
53                 context.startActivity(install);
54             } else {
55                 MLog.d("DownApkReceiver", "下載失敗");
56             }
57         } else {
58             MLog.d("DownApkReceiver", "apkName 為 null");
59         }
60     }
61 }

5. 在清單文件中,注冊該廣播

1         <receiver android:name=".subapps.api.utils.DownApkReceiver">
2             <intent-filter>
3                 <action android:name="android.intent.action.DOWNLOAD_COMPLETE"></action>
4             </intent-filter>
5         </receiver>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM