編輯:關於Android編程
摘自android官方文檔:The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in
the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots. Instances of this class should be obtained through getSystemService(String) by passing DOWNLOAD_SERVICE. Apps that request
downloads through this API should register a broadcast receiver for ACTION_NOTIFICATION_CLICKED to appropriately handle when the user clicks on a running download in a notification or from the downloads UI. Note that the application must have the INTERNET
permission to use this class.
1:manager =(DownloadManager)ctx.getSystemService(ctx.DOWNLOAD_SERVICE); //初始化下載管理器 2: DownloadManager.Request request = new DownloadManager.Request(Uri.parse("www.xxx.com");//創建請求 3:// 設置允許使用的網絡類型,這裡是移動網絡和wifi都可以 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE| DownloadManager.Request.NETWORK_WIFI); // 禁止發出通知,既後台下載 request.setShowRunningNotification(true); // 不顯示下載界面 request.setVisibleInDownloadsUi(true); 4: SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-ddhh-mm-ss"); String date = dateformat.format(new Date()); request.setDestinationInExternalFilesDir(ctx, null,date+".apk");// 設置下載後文件存放的位置--如果目標位置已經存在這個文件名,則不執行下載,所以用date 類型隨機取名。 manager.enqueue(request);// 將下載請求放入隊列 5:在主界面創建下載完成接收器: receiver = new DownloadCompleteReceiver();//創建下載完畢接收器 updateUtils.download();//執行下載 6:不要忘了在這個方法裡注冊廣播接收器,系統下載完成會發送廣播通知 protected void onResume() { registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); super.onResume(); } 7: /** * DownloadManager下載完後 ,DOWNLOAD_SERVICE 會發送廣播提示下載完成 */ public class DownloadCompleteReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals( DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { Toast.makeText(ctx, "下載完成!", Toast.LENGTH_LONG).show(); String fileName = ""; /** * The download manager is a system service that handles long-running HTTP downloads. */ DownloadManager downloadManager = (DownloadManager) ctx .getSystemService(ctx.DOWNLOAD_SERVICE);//從下載服務獲取下載管理器 DownloadManager.Query query = new DownloadManager.Query(); query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);//設置過濾狀態:成功 Cursor c = downloadManager.query(query);// 查詢以前下載過的‘成功文件’ if (c.moveToFirst()) {// 移動到最新下載的文件 fileName = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); } System.out.println("======文件名稱=====" + fileName); File f = new File(fileName.replace("file://", ""));// 過濾路徑 updateUtils.installApk(f);// 開始安裝apk } } }
背景新項目的一個界面需要用到九宮格界面,每個Item包含一張圖片,下面是對應的文字描述,給每個Item設置點擊監聽器,當點擊時跳轉到相應的界面。於是想到使用Android
在做這個計算器的時候,我認為主要分為兩部分:界面設計,功能實現。(效果圖) 界面設計:其實界面設計和功能實現是相互聯系在一起的,我界面怎麼去設計,功
接著上篇文章,現在在通過Android實際開發和源碼再探觀察者模式,listview是我們日常開發中必用的控件,雖然之前就有listview的替代品(recyclervi
實現功能:實現NetMusicListFragment(網絡音樂界面)實現net_music_list_layout.xml(網絡音樂界面UI)使用Jsoup組件請求網絡