GDM Android下载服务工具

 

GDM  

#Goodev Download Manager(谷嘀下载器)

谷嘀下载器(GDM)是由谷嘀团队开发的一款Android下载服务工具,为其他App提供下载功能. Android系统本身具有一个Download Provider可以提供下载管理,但是由于Google并没有对外公开该服务,仅仅在Android内部使用,直接使用不是很方便.

下载: http://goodev.googlecode.com/files/gdm_1.1.apk

使用GDM是相当简单的,只需要下载一个7K大小的Jar包 两行代码就可以实现下载功能了.

  • Jar包下载地址 http://goodev.googlecode.com/files/gdm.jar
  • 源代码: http://goodev.googlecode.com/files/gdm_src.jar
  • 把下载的Jar包导入到Android项目中
  • 使用如下代码即可下载
  • String pkg = "org.goodev.cr"; 
    String clazz = "org.goodev.cr.DownloadReceiver";
    if(DMIntegrator.checkGdmIsInstalled(ReporterTest.this)==null) {
     
    DMIntegrator.startDownload(ReporterTest.this,
    "http://goodev.googlecode.com/files/GoodevHome_2.0.apk", "GoodevHome_2.0",
    "GoodevHome_2.0", "GoodevHome_2.0.apk", DMIntegrator.MIMETYPE_APK,
    Downloads.Impl.VISIBILITY_VISIBLE, pkg, clazz,null);
     
    Toast.makeText(getApplicationContext(), "开始下载...", Toast.LENGTH_LONG).show();
    }

导入如下Class:

org.goodev.dm.DMIntegrator

该Class中具有如下接口:

  • 检测是否已经安装谷嘀下载器的函数
/**
 * 检查GDM是否安装
 * @param activity
 * @return 已经安装 返回 null 否则返回AlertDialog(询问用户是否安装)
 */

public static AlertDialog checkGdmIsInstalled(Activity activity)
  • 检测是否已经安装谷嘀下载器的函数,可以自定义提示信息
/**
 * 检查GDM是否安装,如果没有安装则使用自定义的消息显示安装提示对话框
 * @param activity
 * @param title 安装对话框的标题
 * @param msg 安装对话框的内容
 * @param yes 安装按钮提示文字
 * @param no 不安装按钮提示文字
 * @return 已经安装 返回 null 否则返回AlertDialog(询问用户是否安装)
 */

public static AlertDialog checkGdmIsInstalled(Activity activity,String title,String msg,String yes,String no)
  • 开始下载
/**
 * 开始下载,在调用该函数前请使用{@link #checkGdmIsInstalled(Activity)}来检查是否安装GDM
 * @param context the app Context
 * @param url 下载文件的URL地址,例如 http://goodev.googlecode.com/files/gdm_1.1.apk:
 * @param title 通知的标题
 * @param description 通知的描述
 * @param fileNameHint 保存的文件名提示,如果提供该值且是合法的文件名则会使用该值作为文件名
 * 来保存下载的文件,如果在下载目录(sdcard/goodev/download)已经具有 同名文件则在文件名后面
 * (扩展名前面) 添加数字来区分,例如: gdm-1.apk, gdm-2.apk 等等
 * @param miniType 下载文件的 MIME type .
 * @param visibility 定义通知信息是否显示的标记,详情见下文
 * @param notifPkg 接收下载完成广播信息的包名
 * @param notifClass接收下载完成广播信息的类名,使用如下方式发送广播的Intent
 *  Intent.setClassName(String,String).
 * @param notifClickClass 接收用户点击通知信息的类名, 包名同 notifPkg
 * <b>以上参数中 context和url为必需的 其他可以为null</b>
 */

public static void startDownload(Context  context, String url,String title,String description,
 
String fileNameHint,String miniType,int visibility,
 
String notifPkg,String notifClass,String notifClickClass)

关于 visibility参数的取值说明:

可取如下值:


       
/**
         * 当下载在进行的时候显示下载通知,如果下载完成则隐藏
         */

       
public static final int VISIBILITY_VISIBLE = 0;

       
/**
         * 一直显示下载通知,下载完成后用户点击完成通知则消失
         */

       
public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1;

       
/**
         * 不显示下载通知
         */

       
public static final int VISIBILITY_HIDDEN = 2;

如果需要接收下载完成通知信息,则需要注册一个BroadcastReceiver. 例如:

package org.goodev.cr;

import org.goodev.dm.Downloads;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class DownloadReceiver extends BroadcastReceiver {

       
@Override
       
public void onReceive(Context context, Intent intent) {
               
//action 为发送通知的Intent action,其值为
               
//"goodev.intent.action.DOWNLOAD_COMPLETED" 下载完成通知
               
//或者"goodev.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"
               
//用户点击手机上的Notification(通知)
               
String action = intent.getAction();
               
// 用户下载的数据
               
//例如: content://goodev_downloads/download/1
               
Uri data = intent.getData();
               
//下载文件保存的路径
               
//例如:  /mnt/sdcard/goodev/download/GoodevHome_2-7.0.apk
               
String fileName = intent.getStringExtra(Downloads.Impl.EXTRA_FILENAME);
               
// 如果显示通知,则为通知的id 同时也是下载的id.
               
int id = intent.getIntExtra(Downloads.Impl.EXTRA_ID,-1);
               
if(id != -1) {
                       
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                        notificationMgr
.cancel(id);
               
}
       
}

}

注册一个Receiver可以根据

String action = intent.getAction();

来判断是下载完成事件还是用户点击Notification的事件 然后可以做相应的操作. 比如: 下载完成则复制下载的文件,读取文件内容等. 如果是用户点击Notification事件则显示自定义的下载页面,如果不设置这个值 参数 "notifClickClass " 则使用系统默认的下载管理页面. 见下图.

使用如下参数调用函数:

//Receiver包名
String pkg = "org.goodev.cr";
//Receiver 类名
String clazz = "org.goodev.cr.DownloadReceiver";
if(DMIntegrator.checkGdmIsInstalled(ReporterTest.this)==null) {
       
DMIntegrator.startDownload(ReporterTest.this,
                       
"http://goodev.googlecode.com/files/GoodevHome_2.0.apk", //URL
                       
"GoodevHome", //Notification 标题
                       
"goodev.googlecode.com",//Notification 描述
                       
"GoodevHome_2.0.apk", // 保存的文件名
                       
DMIntegrator.MIMETYPE_APK, //mime type
                       
Downloads.Impl.VISIBILITY_VISIBLE, //Notification显示类型
                        pkg
, //包名
                        clazz
,//类名
                       
null);//使用默认的下载页面响应用户点击Notification事件
       
Toast.makeText(getApplicationContext(), "开始下载...", Toast.LENGTH_LONG).show();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值