DownloadManager是android2.3以后,系统下载的方法。DownloadManager是处理长期运行的HTTP下载的系统服务。客户端可以请求的URI被下载到一个特定的目标文件。客户端将会在后台与http交互进行下载,或者在下载失败,或者连接改变,重新启动系统后重新下载。这个类的实例应该通过getSystemService(字符串)传递DOWNLOAD_SERVICE获得。
DownloadManager用法:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl));//fileUrl下载路径
request.setAllowedNetworkTypes(Request.NETWORK_WIFI
|Request.NETWORK_MOBILE);//设置在什么网络状态下面能够下载软件
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setAllowedOverRoaming(true);//是否允许网络漫游
request.setDestinationUri(Uri.fromFile(new File(filePath)));//设置文件存放目录,filePath是保存文件的路径
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = downloadManager.enqueue(request);//返回下载的Id,可以根据id查询下载的状态,下载进度
一:用ContentObserver来获取进度
MainActivty的主要代码:
public class MainActivity extends Activity {
private Button mDownloadFileBtn;
private DownloadObserver mDownloadObserver;
private long downId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDownloadFileBtn = (Button) findViewById(R.id.download_btn);
mDownloadFileBtn.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
downId = DownloadFile.downloadFile(MainActivity.this,
"http://n3.f.imoxiu.com/9ccf9a36960340713c5d3a74ec1fbf86b107fa6d");
mDownloadObserver = new DownloadObserver(mHandler,MainActivity.this, downId);
getContentResolver().registerContentObserver(Uri.parse("content://downloads/"),
true,
mDownloadObserver);
}
});
}
private Handler mHandler = new Handler() {
public void handleMessage(final Message msg) {
super.handleMessage(msg);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mDownloadFileBtn.setText(msg.what + "%");
}
});
}
};
}
DownloadObserver的代码:
public class DownloadObserver extends ContentObserver {
private Handler mHandler;
private Context mContext;
private int progress;
private DownloadManager mDownloadManager;
private DownloadManager.Query query;
private Cursor cursor;
@SuppressLint("NewApi")
public DownloadObserver(Handler handler, Context context, long downId) {
super(handler);
// TODO Auto-generated constructor stub
this.mHandler = handler;
this.mContext = context;
mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
query = new DownloadManager.Query().setFilterById(downId);
}
@SuppressLint("NewApi")
@Override
public void onChange(boolean selfChange) {
// 每当/data/data/com.android.providers.download/database/database.db变化后,触发onCHANGE,开始具体查询
super.onChange(selfChange);
//
boolean downloading = true;
while (downloading) {
cursor = mDownloadManager.query(query);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
progress = (int) ((bytes_downloaded * 100) / bytes_total);
cursor.close();
mHandler.sendEmptyMessageDelayed(progress, 100);
if (cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
}
}
}
这种方法虽然能够获取到下载的进度,但是Button上面字体的内容还是没有改变,但是button.getText()的返回值还是改变的,具体是什么原因导致,button内容只显示100%,我现在还没有找到根本原因。