首页 > 移动平台 > 详细

Android downloadmanger 2之对于下载完成和点击下载通知的监听

时间:2014-06-15 15:31:38      阅读:392      评论:0      收藏:0      [点我收藏+]

监听下载完成:

要想在下载完成后收到一个通知,需要注册一个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);


Android downloadmanger 2之对于下载完成和点击下载通知的监听,布布扣,bubuko.com

Android downloadmanger 2之对于下载完成和点击下载通知的监听

原文:http://blog.csdn.net/howlaa/article/details/30747973

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!