Android downloadmanger 2のダウンロード完了とクリックダウンロード通知の傍受

1692 ワード

ダウンロードの傍受完了:
ダウンロード完了後に通知を受信するには、ACTIONを受信するためにReceiverを登録する必要があります.DOWLOAD_COMPLETE放送.EXTRA_が含まれますDOWNLOAD_ダウンロードが完了した参照IDを含むID extra:
	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);