編輯:關於Android編程
DownloadManager是android提供的一個下載管理器
缺點:不支持斷點續傳
使用方法:
創建實例:
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
創建下載任務:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
//指定保存地址
request.setDestinationInExternalPublicDir(chuyi, meizhi.apk);
//設置允許下載的網絡狀況
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//設置通知欄的行為
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//通過id唯一標識此下載任務
long id = manager.enqueue(request);
刪除下載任務:
manager.remove(id);
查詢下載任務:
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cursor = manager.query(query);
if(cursor.moveToFirst()){
String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
String fileUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Toast.makeText(context,下載完成...name:+filename+,uri:+fileUri,Toast.LENGTH_SHORT).show();
}else {
//TODO
}
cursor.close();
查詢下載進度:
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor c = null;
try {
c = downloadManager.query(query);
if (c != null && c.moveToFirst()) {
int downloadedBytes = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int totalBytes = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
int state = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
}
} finally {
if (c != null) {
c.close();
}
}
監聽下載結束通知:
可以通過接收DownloadManager.ACTION_DOWNLOAD_COMPLETE廣播來監聽下載結束的通知
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
mReceiver = new BroadcastReceiver(){
public void onReceive(Context c,Intent i){
long downId = i.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
}
}
context.registerReceiver(mReceiver,filter);
監聽下載進度:
主要有三種方案:1.FileReceiver 2.ContentObserver 3.定時任務
class DownloadChangeObserver extends ContentObserver {
public DownloadChangeObserver(){
super(handler);
}
@Override
public void onChange(boolean selfChange) {
//查詢進度
}
}
//in activity
private DownloadChangeObserver downloadObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download_manager_demo);
……
downloadObserver = new DownloadChangeObserver();
}
@Override
protected void onResume() {
super.onResume();
/** observer download change **/
getContentResolver().registerContentObserver(DownloadManagerPro.CONTENT_URI, true,
downloadObserver);
}
@Override
protected void onPause() {
super.onPause();
getContentResolver().unregisterContentObserver(downloadObserver);
}
上面這種做法可能對性能有些損耗,因為會不斷觸發onChange
推薦使用ScheduledExecutorService
public static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
Runnable command = new Runnable() {
@Override
public void run() {
updateView();
}
};
scheduledExecutorService.scheduleAtFixedRate(command, 0, 3, TimeUnit.SECONDS);
本文實現如下幾個界面之間的平移動畫實現分析:導航界面移動過程中,平移動畫上一頁移入動畫 (-屏幕寬度,y)------>(0,y)上一頁移出動畫 (0,y)----
Android提供alert、prompt、pick-list,單選、多選,progress、time-picker和date-picker對話框,並提供自定義的dial
Cardview配合ImageView顯示圓形圖效果圖:剛在看自定義View的知識點時,突然想起來,如果CardView寬高相等,CardView設置圓角的半徑為寬高的一
當我們拿到一台Android的智能手機,從打開開關,到我們可以使用其中的app時,這個啟動過程到底是怎麼樣的? 系統上電 當給Android系統上電,CPU復位之後,