Android學習筆記:通過Android之Service實現檔案斷點續傳下載

NO IMAGE

   今天工作主要是改BUG,等版本上線,忙裡偷閒,學習了下http://www.imooc.com/上的Android斷點下載視屏,邊看邊寫,順便寫個筆記!感謝老師的無私分享!

—————-操作入口,主Activity—————————-

public class MainActivity extends Activity {
private TextView tvFileName = null;// 檔名
private ProgressBar pbFile = null;// 下載進度、
private Button btnStart = null, btnStop = null;// 開始和暫停
private FileBean fileBean = null;
private static final String URL = "http://downfile.downcc.com/file/exe/cmd.rar";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileBean = new FileBean(URL, 0, "cmd.rar", 0, 0);
initViews();
initEvents();
}


/**
* 獲取控制元件
* @description:
* @date 2015-9-24 下午1:24:08
*/
private void initViews() {
this.tvFileName = (TextView) findViewById(R.id.tvFileName);
this.pbFile = (ProgressBar) findViewById(R.id.pbFile);
this.btnStart = (Button) findViewById(R.id.btnStart);
this.btnStop = (Button) findViewById(R.id.btnStop);
tvFileName.setText("cmd.rar");
pbFile.setMax(100);
}


/**
* 按鈕事件處理
* @description:
* @date 2015-9-24 下午1:24:24
*/
private void initEvents() {
this.btnStart.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, LoadDownService.class);
startIntent.setAction(LoadDownService.STATR_ACTION);
startIntent.putExtra(LoadDownService.FILE_INTENT, fileBean);
startService(startIntent);
}
});
this.btnStop.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Intent stopIntent = new Intent(MainActivity.this, LoadDownService.class);
stopIntent.setAction(LoadDownService.STOP_ACTION);
stopIntent.putExtra(LoadDownService.FILE_INTENT, fileBean);
startService(stopIntent);
}
});
// 在程式碼中註冊廣播
IntentFilter filter = new IntentFilter();
filter.addAction(LoadDownService.UPDATE_ACTION);
registerReceiver(mReceiver, filter);
}


BroadcastReceiver mReceiver = new BroadcastReceiver() {


@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(LoadDownService.UPDATE_ACTION)) {
int nowProgress = intent.getIntExtra(LoadDownService.LOAD_PROGRESS, 0);
pbFile.setProgress(nowProgress);
if (pbFile.getProgress() >= 100) {
Toast.makeText(context, "下載完成!", Toast.LENGTH_SHORT).show();
}
}
}


};
}

————–對應的佈局檔案:—————————————————————-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@ id/tvFileName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <ProgressBar
        android:id="@ id/pbFile"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        android:gravity="right" >

        <Button
            android:id="@ id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開始"
            android:textSize="16sp" />

        <Button
            android:id="@ id/btnStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暫停"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

————-涉及到的實體類:—————————————————————–

/**
 * 下載的檔案對應實體bean
 * @description:
 * @date 2015-9-24 下午1:28:09
 */

public class FileBean implements Serializable {
private static final long serialVersionUID = 1L;
private String url;// 下載檔案對應的url
private int id;// 檔案對應的id
private String fileName;// 檔名稱
private int length;// 檔案大小
private int progress;// 下載的進度


public FileBean() {
}


public FileBean(String url, int id, String fileName, int length, int progress) {
super();
this.url = url;
this.id = id;
this.fileName = fileName;
this.length = length;
this.progress = progress;
}


public String getUrl() {
return url;
}


public void setUrl(String url) {
this.url = url;
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getFileName() {
return fileName;
}


public void setFileName(String fileName) {
this.fileName = fileName;
}


public int getLength() {
return length;
}


public void setLength(int length) {
this.length = length;
}


public int getProgress() {
return progress;
}


public void setProgress(int progress) {
this.progress = progress;
}


@Override
public String toString() {
return "FileBean [url="   url   ", id="   id   ", fileName="   fileName   ", length="   length   ", progress="   progress   "]";
}

}
----------------------------下載檔案執行緒實體類--------------------------------------------------
/**
 * 下載檔案執行緒資訊實體bean
 * @description:
 * @date 2015-9-24 下午1:32:21
 */
public class ThreadBean implements Serializable {
private static final long serialVersionUID = 1L;
private int id;// 執行緒對應id
private String url;// 執行緒下載對應url
private int start;// 開始位置
private int end;// 結束位置
private int progress;// 當前執行緒下載進度


public ThreadBean() {


}


public ThreadBean(int id, String url, int start, int end, int progress) {
super();
this.id = id;
this.url = url;
this.start = start;
this.end = end;
this.progress = progress;
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getUrl() {
return url;
}


public void setUrl(String url) {
this.url = url;
}


public int getStart() {
return start;
}


public void setStart(int start) {
this.start = start;
}


public int getEnd() {
return end;
}


public void setEnd(int end) {
this.end = end;
}


public int getProgress() {
return progress;
}


public void setProgress(int progress) {
this.progress = progress;
}


@Override
public String toString() {
return "ThreadBean [id="   id   ", url="   url   ", start="   start   ", end="   end   ", progress="   progress   "]";
}

}

————-資料庫操作相關——————————————————-

/**
* 資料庫操作工具類
* @description:
* @date 2015-9-24 下午2:37:22
*/
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "load_db";
private static final int DB_VERSION = 1;
private static final String CREATE_TABLE = "create table file_info(_id integer primary key,thread_id integer,url text,start integer,end integer,progress integer)";
private static final String DROP_TABLE = "drop table if exists file_info";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
db.execSQL(CREATE_TABLE);
}
}
/**
* 資料操作介面具體實現
* @description:
* @date 2015-9-24 下午2:51:25
*/
public class DBOperatorImpl implements LoadDAO {
private DBHelper mHelper = null;
public DBOperatorImpl(Context context) {
mHelper = new DBHelper(context);
}
@Override
public void insert(ThreadBean bean) {
SQLiteDatabase db = mHelper.getWritableDatabase();
db.execSQL("insert into file_info(thread_id,url,start,end,progress) values(?,?,?,?,?)", new Object[] { bean.getId(), bean.getUrl(), bean.getStart(), bean.getEnd(), bean.getProgress() });
db.close();
}
@Override
public void delete(String url, int id) {
SQLiteDatabase db = mHelper.getWritableDatabase();
db.execSQL("delete from file_info where url=? and thread_id=?", new Object[] { url, id });
db.close();
}
@Override
public void update(String url, int id, int progress) {
SQLiteDatabase db = mHelper.getWritableDatabase();
db.execSQL("update file_info set progress=? where url=? and wher id=?", new Object[] { progress, url, id });
db.close();
}
@Override
public List<ThreadBean> query(String url) {
List<ThreadBean> threadList = new ArrayList<ThreadBean>();
SQLiteDatabase db = mHelper.getWritableDatabase();
Cursor mCursor = db.rawQuery("select * from file_info where url=?", new String[] { url });
while (mCursor.moveToNext()) {
ThreadBean bean = new ThreadBean();
bean.setId(mCursor.getInt(mCursor.getColumnIndex("thread_id")));
bean.setUrl(mCursor.getString(mCursor.getColumnIndex("url")));
bean.setStart(mCursor.getInt(mCursor.getColumnIndex("start")));
bean.setEnd(mCursor.getInt(mCursor.getColumnIndex("end")));
bean.setProgress(mCursor.getInt(mCursor.getColumnIndex("progress")));
threadList.add(bean);
}
mCursor.close();
db.close();
return threadList;
}
@Override
public boolean isExistThread(String url, int id) {
SQLiteDatabase db = mHelper.getWritableDatabase();
Cursor mCursor = db.rawQuery("select * from file_info where url=? and thread_id=?", new String[] { url, String.valueOf(id) });
boolean isExist = mCursor.moveToNext();
mCursor.close();
db.close();
return isExist;
}
}

/**
 * 資料庫操作介面類
 * @description:
 * @date 2015-9-24 下午2:44:44
 */

public interface LoadDAO {
void insert(ThreadBean bean);// 插入資料
void delete(String url, int id);// 刪除資料
void update(String url, int id, int progress);// 更新資料
List<ThreadBean> query(String url);// 查詢url對應的執行緒資訊
boolean isExistThread(String url, int id);// 判斷執行緒是否已經存在
}

———————–下載操作service——————————-

/**
* 檔案下載操作Service
* @description:
* @date 2015-9-24 下午1:40:02
*/
public class LoadDownService extends Service {
public static final String STATR_ACTION = "start_action";
public static final String STOP_ACTION = "stop_action";
public static final String UPDATE_ACTION = "update_action";
public static final String FILE_INTENT = "file_intent";
public static final String SAVE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()   "/downLoads/";
private FileBean fileBean;
public static final int MESSAGE_INIT = 0;
public static final String LOAD_PROGRESS = "load_progress";
private LoadTask mTask=null;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MESSAGE_INIT:
FileBean bean = (FileBean) msg.obj;
mTask=new LoadTask(LoadDownService.this, bean);
mTask.getLoadThreadInfo();
break;
}
};
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 根據Action來獲取傳遞過來的檔案資訊
if (STATR_ACTION.equals(intent.getAction())) {
fileBean = (FileBean) intent.getSerializableExtra(FILE_INTENT);
new LoadThread(fileBean).start();// 啟動執行緒
}
else if (STOP_ACTION.equals(intent.getAction())) {
fileBean = (FileBean) intent.getSerializableExtra(FILE_INTENT);
if(null!=mTask){
mTask.isPause=true;
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
/**
* 檔案下載的執行緒(涉及到網路下載等耗時操作,我們都需要使用多執行緒來操作)
* @description:
* @author ldm
* @date 2015-9-24 下午2:05:03
*/
class LoadThread extends Thread {
private FileBean mFileBean;
public LoadThread(FileBean mFileBean) {
this.mFileBean = mFileBean;
}
@Override
public void run() {
HttpURLConnection conn = null;
RandomAccessFile raf = null;
int length = -1;
try {
// 第一步:連線網路檔案
URL url = new URL(mFileBean.getUrl());
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);// 設定超時
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpStatus.SC_OK) {// 判斷請求成功
// 第二步:獲取到檔案內容的大小 (長度)
length = conn.getContentLength();
}
if (length <= 0) { return; }
File dir = new File(SAVE_PATH);
if (!dir.exists()) {
dir.mkdir();
}
// 第三步:建立本地檔案
File file = new File(dir, mFileBean.getFileName());
raf = new RandomAccessFile(file, "rwd");// 可以讀,寫,刪除
// 第四步:設定檔案大小
raf.setLength(length);
mFileBean.setLength(length);
mHandler.obtainMessage(MESSAGE_INIT, mFileBean).sendToTarget();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
conn.disconnect();
raf.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
/**
* 實現下載操作
* @description:
* @date 2015-9-24 下午5:06:38
*/
public class LoadTask {
private Context mContext = null;
private FileBean mFileBean = null;
private LoadDAO mLoadDAO = null;
private int progress = 0;
public boolean isPause = false;// 下載是否暫停
public LoadTask(Context mContext, FileBean mFileBean) {
this.mContext = mContext;
this.mFileBean = mFileBean;
this.mLoadDAO = new DBOperatorImpl(mContext);
}
public void getLoadThreadInfo() {
List<ThreadBean> threadInfos = mLoadDAO.query(mFileBean.getUrl());
ThreadBean threadBean = null;
if (threadInfos.size() == 0) {
threadBean = new ThreadBean(0, mFileBean.getUrl(), 0, mFileBean.getLength(), 0);
}
else {
threadBean = threadInfos.get(0);
}
// 建立執行緒下載
new LoadTaskThread(threadBean).start();
}
class LoadTaskThread extends Thread {
private ThreadBean mThreadBean = null;
public LoadTaskThread(ThreadBean mThreadBean) {
this.mThreadBean = mThreadBean;
}
@Override
public void run() {
// 第一步:向資料庫插入執行緒資訊
if (!mLoadDAO.isExistThread(mThreadBean.getUrl(), mThreadBean.getId())) {// 如果當前執行緒不存在,則插入當前 執行緒
mLoadDAO.insert(mThreadBean);
}
HttpURLConnection conn = null;
RandomAccessFile raf = null;
try {
URL url = new URL(mThreadBean.getUrl());
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
// 第二步:設定執行緒下載位置
int start = mThreadBean.getStart()   mThreadBean.getProgress();
conn.setRequestProperty("Range", "bytes="   start   "-"   mThreadBean.getEnd());
// 第三步:設定檔案寫入位置
File file = new File(LoadDownService.SAVE_PATH, mFileBean.getFileName());
raf = new RandomAccessFile(file, "rwd");
raf.seek(start);// seek()方法目的:在讀寫的時候路過設定好的位元組數,從下一個位元組數開始讀寫
Intent intent = new Intent(LoadDownService.UPDATE_ACTION);
progress  = mThreadBean.getProgress();
// 第四步:進入下載
if (conn.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) {// 判斷網路請求狀態 OK
// step01:讀取資料
InputStream inputStream = conn.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
long time = System.currentTimeMillis();
while ((len = inputStream.read(buffer)) != -1) {
// step02:寫入檔案
raf.write(buffer, 0, len);
// step03:把當前的下載進度告訴Activity
progress  = len;
if (System.currentTimeMillis() - time > 500) {
time = System.currentTimeMillis();
intent.putExtra(LoadDownService.LOAD_PROGRESS, progress * 100 / mFileBean.getLength());
mContext.sendBroadcast(intent);
}
// step04:下載暫停時,儲存下載進度
if (isPause) {
mLoadDAO.update(mThreadBean.getUrl(), mThreadBean.getId(), progress);
return;
}
}
// 刪除執行緒資訊
mLoadDAO.delete(mThreadBean.getUrl(), mThreadBean.getId());
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
raf.close();
conn.disconnect();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

最後不要忘記在AndroidManifest.xml中新增 相應許可權 及Service元件。