首页 > 代码库 > Android downloadmanger 2之对于下载完成和点击下载通知的监听
Android downloadmanger 2之对于下载完成和点击下载通知的监听
监听下载完成:
要想在下载完成后收到一个通知,需要注册一个Receiver来接收ACTION_DOWLOAD_COMPLETE广播。它将包含一个EXTRA_DOWNLOAD_ID extra,其中包含了已经完成下载的引用ID:
public void funclick(View view){ String serviceString = Context.DOWNLOAD_SERVICE; DownloadManager downloadManager; downloadManager = (DownloadManager)getSystemService(serviceString); Uri uri = Uri.parse("http://img00.hc360.com/it/201206/201206121034589404.jpg"); DownloadManager.Request request = new Request(uri); request.setAllowedNetworkTypes(Request.NETWORK_WIFI); downloadManager.getRecommendedMaxBytesOverMobile(getApplicationContext()); final long myreference = downloadManager.enqueue(request); IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE ); BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1); if(reference == myreference){ //对下载的文件进行一些操作 } } }; registerReceiver(receiver, filter); }
监听点击下载通知栏:
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED); BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String extraID = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS; long[] references = intent.getLongArrayExtra(extraID); for(long reference : references){ if(reference == myreference){ //对下载的文件进行一些操作 } } } }; registerReceiver(receiver, filter);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。