Because the project requires a download management module, we have been studying the implementation of File Download recently, and have finally finished it through continuous efforts! In File Download management, we need to save the file information, including the file size, file name, and type. Now we need to use the database. The android system provides us with a lightweight database SQLite. For how to use SQLite, refer to the related video of mars! The following is an example of my project for your reference.
First, define a DBHelper that inherits SQLiteOpenHelper:
[Java]
Package com. cloay. down. db;
Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
/**
* DBHelper that saves the downloaded file information
* DBHelper. java
* @ Author cloay
* 2011-11-18
*/
Public class DBHelper extends SQLiteOpenHelper {
// Download. db --> Database Name
Public DBHelper (Context context ){
Super (context, "download. db", null, 1 );
}
/**
* Create a download_info table under the download. db database to store the download information.
* State defines four download statuses: initialization status 1, download status 2, pause status 3, and download completion.
*/
@ Override
Public void onCreate (SQLiteDatabase db ){
Db.exe cSQL ("create table download_info (_ id integer primary key autoincrement, url char ,"
+ "File_size integer, compelete_size integer, state integer )");
}
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
}
}
Then define a DBHelperUtil to implement some common database operation functions:
[Java]
Package com. cloay. down. utils;
Import java. util. ArrayList;
Import java. util. List;
Import com. cloay. down. db. DBHelper;
Import com. cloay. down. entity. FileInfo;
Import android. content. Context;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteDatabase;
/**
* Database tool class for searching, deleting, and updating databases
* DBHelperUtil. java
* @ Author cloay
* 2011-11-18
*/
Public class DBHelperUtil {
Private DBHelper dbHelper;
Public DBHelperUtil (Context context ){
DbHelper = new DBHelper (context );
}
/**
* Check whether data exists in the database.
* @ Param urlstr
* Download link
* @ Return
*/
Public boolean isHasInfors (String urlstr ){
SQLiteDatabase database = dbHelper. getReadableDatabase ();
String SQL = "select count (*) from download_info where url =? ";
Cursor cursor = database. rawQuery (SQL, new String [] {urlstr });
Cursor. moveToFirst ();
Int count = cursor. getInt (0 );
Cursor. close ();
CloseDb ();
If (count = 0 ){
Return true;
}
Else {
Return false;
}
}
/**
* Obtain the number of data in the database www.2cto.com
*/
Public int getLoadInfoCount (){
Int I = 0;
SQLiteDatabase database = dbHelper. getReadableDatabase ();
String SQL = "select count (*) from download_info ";
Cursor cursor = database. rawQuery (SQL, new String [] {});
While (cursor. moveToNext ()){
I ++;
}
CloseDb ();
Return I;
}
/**
* Query all data in the database
*/
Public List <FileInfo> getLoadInfos (){
List <FileInfo> fileInfos = new ArrayList <FileInfo> ();
SQLiteDatabase database = dbHelper. getReadableDatabase ();
String SQL = "select url, file_size, compelete_size, state from download_info ";
Cursor cursor = database. rawQuery (SQL, new String [] {});
While (cursor. moveToNext ()){
FileInfo info = new FileInfo (cursor. getString (0), cursor. getInt (1), cursor. getInt (2), cursor. getInt (3 ));
FileInfos. add (info );
}
Cursor. close ();
CloseDb ();
Return fileInfos;
}
/**
* Save the download details.
* @ Param info
* Download file information
*/
Public void saveInfos (FileInfo info ){
SQLiteDatabase database = dbHelper. getWritableDatabase ();
String SQL = "insert into download_info (file_size, compelete_size, url, state) values (?,?,?,?) ";
Object [] bindArgs = {info. getFileSize (), info. getCompleteSize (), info. getUrlstring (), info. getState ()};
Database.exe cSQL (SQL, bindArgs );
}
/**
* Get the download details.
* @ Param urlstr
* Download url
* @ Return
*/
Public FileInfo getInfo (String urlstr ){
FileInfo info = null;
SQLiteDatabase database = dbHelper. getReadableDatabase ();
String SQL = "select file_size, compelete_size, url, state from download_info where url =? ";
Cursor cursor = database. rawQuery (SQL, new String [] {urlstr });
While (cursor. moveToNext ()){
Info = new FileInfo (cursor. getString (2), cursor. getInt (0), cursor. getInt (1), cursor. getInt (3 ));
}
Cursor. close ();
CloseDb ();
Return info;
}
/**
* Update the download information in the database.
*/
Public void updataInfos (int compeleteSize, int state, String urlstr ){
SQLiteDatabase database = dbHelper. getReadableDatabase ();
String SQL = "update download_info set compelete_size = ?, State =? Where url =? ";
Object [] bindArgs = {compeleteSize, state, urlstr };
Database.exe cSQL (SQL, bindArgs );
}
/**
* Shut down the database.
*/
Public void closeDb (){
DbHelper. close ();
}
/**
* After the download is complete, the data in the database will be deleted.
*/
Public void delete (String url ){
SQLiteDatabase database = dbHelper. getReadableDatabase ();
Database. delete ("download_info", "url =? ", New String [] {url });
}
}
Through database operations, we can save the current information about the file and manage the file download.
Please leave a message if you have any questions!
From Cloay's column