码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-06-15 15:32:44      阅读:618      评论:0      收藏:0      [点我收藏+]

标签:android   class   blog   code   java   http   

监听下载完成:

要想在下载完成后收到一个通知,需要注册一个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之对于下载完成和点击下载通知的监听

标签:android   class   blog   code   java   http   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!