DownLoad 1 file từ InternetTrong bài viết này mình sẽ giới thiệu với các bạn về cách xây dựng 1 bộ download Manger full , nhưng do còn hạn chế nên mình chỉ download từ 1 direct link trên
Trang 1DownLoad 1 file từ Internet
Trong bài viết này mình sẽ giới thiệu với các bạn về cách xây dựng 1 bộ download Manger full , nhưng do còn hạn chế nên mình chỉ download từ 1 direct link trên code Các bạn hoàn toàn có thể sử dụng bài demo này để thêm vào ứng dụng riêng của các bạn Bài demo của mình làm theo các bước như sau:
1/ Tạo 1 Project :
Project name: DownloadManagerDemo
Build Target: Android 2.3.3
Application name: DownloadManagerDemo
Package name: com.dac DownloadManagerDemo
Create Activity: MainActivity.java
2/ Tiếp theo các bạn thiết kế giao diện trong file main.xml như sau:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<Button android:onClick="doClick" android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3/ Và để cho ứng dụng có thể truy cập internet cũng như download được thì các bạn thêm vào file AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android= http://schemas.android.com/apk/res/android"
package="com.dac.DownloadManagerDemo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity"
android:label="@string/app_name">
Trang 2<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
4/ Và rồi chỉ còn việc viết code cho file MainActivity.java:
package com.dac.DownloadManagerDemo
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.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
protected static final String TAG = "DownloadMgr";
private DownloadManager dMgr;
private TextView tv;
private long downloadId;
/** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv);
}
@Override
protected void onResume() {
super.onResume();
dMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Trang 3public void doClick(View view) {
DownloadManager.Request dmReq = new DownloadManager.Request(
Uri.parse(
"http://download1440.mediafire.com/nyewp2g1dueg/41zhg2echx2/Picachu.exe")); //Duong dan de download
dmReq.setTitle("Platform Tools");
dmReq.setDescription("Download Game Dao Vang");
dmReq.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE);
IntentFilter filter = new
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(mReceiver, filter);
downloadId = dMgr.enqueue(dmReq);
tv.setText("Download started (" + downloadId + ")");
}
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
long doneDownloadId =
extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
tv.setText(tv.getText() + "\nDownload finished (" +
doneDownloadId + ")");
if(downloadId == doneDownloadId)
Log.v(TAG, "Our download has completed.");
}
};
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
dMgr = null;
}
}
Khi các bạn debug:
Trang 4Bạn bấm vào nút Start và kéo cái cửa sổ Notice bên trên xuống sẽ thấy chương trình Download đang làm việc :
Trang 5Và các bạn hoàn toàn có thể kiểm tra lại trong mục download trên menu chính của Virtual Devices (chú ý là tên mặc định nên dù down 2 file khác nhau nhưng trên bảng download sẽ thể hiện cùng tên):
Mọi ý kiến đóng góp các bạn vui lòng gữi bài viết về forum
http://forum.laptrinhdidong.vn/ , rất mong nhận được sự phản hồi từ các bạn.