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);


posted @ 2017-04-19 09:38  jzdwajue  阅读(251)  评论(0编辑  收藏  举报