public class CompleteReceiver extends BroadcastReceiver
{android
private DownloadManager downloadManager = null;網絡
@Override
public void onReceive(Context context, Intent intent)
{
try {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
{
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); // TODO
// 判斷這個id與以前的id是否相等,若是相等說明是以前的那個要下載的文件
Query query = new Query();
query.setFilterById(id);
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor cursor = downloadManager.query(query);app
int columnCount = cursor.getColumnCount();
String path = null; // TODO 這裏把全部的列都打印一下,有什麼需求,就怎麼處理,文件的本地路徑就是path
String local_fileName=null;
while (cursor.moveToNext())
{
for (int j = 0; j < columnCount; j++)
{
String columnName = cursor.getColumnName(j);
String string = cursor.getString(j);
// 輸出
output(context, columnName, string);
if (columnName.equals("local_uri"))
{
path = string;
} else if (columnName.equals("local_filename")){
local_fileName=string;
} else {
//
}
}
}
cursor.close();
// 若是sdcard不可用時下載下來的文件,那麼這裏將是一個內容提供者的路徑,這裏打印出來,有什麼需求就怎麼樣處理
if (path != null && path.startsWith("content:"))
{
cursor = context.getContentResolver().query(Uri.parse(path), null, null, null, null);
columnCount = cursor.getColumnCount();
while (cursor.moveToNext())
{
for (int j = 0; j < columnCount; j++)
{
String columnName = cursor.getColumnName(j);
String string = cursor.getString(j);
// 輸出
output(context, columnName, string);
}
}
cursor.close();
}
if (Common.isNotNull(local_fileName)) {
String worksheetId = local_fileName.substring(local_fileName.lastIndexOf("/")+1,local_fileName.lastIndexOf("."));
if (local_fileName != null && local_fileName.substring(local_fileName.lastIndexOf(".")+1).equals("apk"))
{
install(context, local_fileName);
}else if (local_fileName!= null && local_fileName.substring(local_fileName.lastIndexOf(".")+1).equals("db") && !worksheetId.equalsIgnoreCase(IConstant.OFFLINE_PUB_DB_NAME)) {
new WorksheetService().updateOfflineStatues(context, worksheetId, "1");
Toast.makeText(context, "離線數據包下載已完成", Toast.LENGTH_SHORT).show();
} else {
//
}
}
} else if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED))
{
//點擊了notification後的操做
}
} catch (Exception er) {
Log.error("資源下載異常", er);
}
}
ide
public class DownLoadNewApk {
private static final Logger Log = LoggerFactory.getLogger(WService.class);this
public DownLoadNewApk(Context context) {
this.context = context;
}url
private Context context;ip
public void getDownLoad(String url) {
try {
@SuppressWarnings("static-access")
DownloadManager downloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
url = url.replace("HTTP", "http");
Uri uri = Uri.parse(url);
Request request = new Request(uri);資源
// 設置容許使用的網絡類型,這裏是移動網絡和wifi均可以
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);get
// 禁止發出通知,既後臺下載,若是要使用這一句必須聲明一個權限:android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
// request.setShowRunningNotification(false);string
// 不顯示下載界面
request.setVisibleInDownloadsUi(true);
request.setDescription("下載中...");
request.setMimeType("application/com.trinea.download.file");
/* * 設置下載後文件存放的位置,若是sdcard不可用,那麼設置這個將報錯,所以最好不設置。 * 若是sdcard可用,下載後的文件在/mnt/sdcard/Android/data/packageName/files目錄下面。 * 若是sdcard不可用,設置了下面這個將報錯,不設置,下載後的文件在/cache這個 目錄下面 */ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { request.setDestinationInExternalPublicDir("/download/", url.substring(url.lastIndexOf("/") + 1)); } downloadManager.enqueue(request); // TODO 把id保存好,在接收者裏面要用,最好保存在Preferences裏面 } catch (Exception er) { Log.error("", er); } }