Month: February 2012

  • Downlodas 之 ByUri.startDownloadByUri

    很久以前写过一篇《How to Use Android Downloads Provider》,到了Android 2.2(froyo),Android 提供了更优雅的接口:

    public static long startDownloadByUri(Context context,
                                          String url,
                                          String cookieData,
                                          boolean showDownload,
                                          int downloadDestination,
                                          boolean allowRoaming,
                                          boolean skipIntegrityCheck,
                                          String title,
                                          String notification_package,
                                          String notification_class,
                                          String notification_extras)

    当然这些接口都是hide的,做ROM系统开发的才可以直接使用,用mmm方式编译。签于这个原因,一些国内第三方电子市场直接拿Downloads组件的代码修改成自己的提供使用。代码实现在:

    [deli@violet frameworks]$ find . -type f -name Downloads.java
    ./base/core/java/android/provider/Downloads.java
    ./base/core/java/android/net/Downloads.java

    还有 packages/providers/DownloadProvider

    startDownloadByUri 即这个函数很容易理解,有网友给我发email说不知道如何使用,我贴出系统升级使用的代码片段,希望对大家有帮助。

    [deli@violet update]$ cat Download.java

    /*
     * Copyright (C) 2010 lytsing.org
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.google.android.gsf.update;
    
    import com.google.android.gsf.R;
    
    import android.content.Context;
    import android.util.Log;
    import android.net.Downloads;
    
    public class Download {
    
        private final Context mContext;
    
        private String mNotificationClass;
    
        private String mNotificationPackage;
    
        public Download(Context context) {
            mContext = context;
            mNotificationPackage = context.getPackageName();
            mNotificationClass = SystemUpdateService.Receiver.class.getName();
        }
    
        public boolean downloadUpdate(String url, boolean showDownload, boolean allowRoaming) {
            removeAllDownloads();
            try {
                if (Downloads.ByUri.startDownloadByUri(
                            mContext,
                            url,
                            null,
                            showDownload,
                            2,
                            allowRoaming,
                            true,
                            mContext.getString(R.string.system_update_downloading_notification_title),
                            mNotificationPackage,
                            mNotificationClass,
                            null)== -1) {
                    Log.w("update.Download", "Could not insert download entry into provider");
                    return false;
                } 
    
                Log.i("update.Download",new StringBuilder()
                        .append("Started a new update download: ").append(url).toString());
            } catch (Exception e) {
                Log.e("update.Download", "Could not start update download", e);
                return false;
            }
    
            return true;
        }
    
        public Downloads.StatusInfo getStatus(String url) {
            return Downloads.ByUri.getStatus(mContext, url, 24 * 3600 * 1000);
        }
    
        public void removeAllDownloads() {
            Log.v("update.Download", "deleting all update downloads");
            try {
                Downloads.ByUri.removeAllDownloadsByPackage(mContext, mNotificationPackage,
                        mNotificationClass);
            } catch (Exception ex) {
                Log.e("update.Download", "Couldn\'t delete downloads", ex);
            }
        }
    }