安卓内部下载

package com.example.aexercise927;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class DownTest extends Activity {
	TextView textView;
	DownloadManager downloadManager;
	String urlString = "http://dlsw.baidu.com/sw-search-sp/soft/3a/12350/QQ6.4.1411525511.exe";
	long id;

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.downtest);
		textView = (TextView) findViewById(R.id.textView1);
	}

	public BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Bundle bundle = intent.getExtras();
			long downId = bundle.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
			textView.setText(textView.getText() + "\nfinishId:" + downId);
			if (downId == id) {
				textView.setText(textView.getText() + "\nover!");
			}
		}
	};

	public void myAction(View v) {
		switch (v.getId()) {
		case R.id.button3:
			DownloadManager.Request request = new DownloadManager.Request(
					Uri.parse(urlString));
			request.setTitle("文件下载");
			request.setDescription("QQ最新版下载");
			IntentFilter intentFilter = new IntentFilter(
					DownloadManager.ACTION_DOWNLOAD_COMPLETE);
			registerReceiver(receiver, intentFilter);
			id = downloadManager.enqueue(request);
			textView.setText(textView.getText() + "\nstart...!");
			break;

		default:
			break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(receiver);
		downloadManager = null;

	}
}

你可能感兴趣的:(安卓内部下载)