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


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

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

上一篇:Android学习笔记(四十):Preference的使用


下一篇:Android Binder分析二:Natvie Service的注冊