Android-Download(下載)項(xiàng)目詳解-創(chuàng)新互聯(lián)

Download(下載) 項(xiàng)目 詳解


成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)灤平,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108

本文地址: http://blog.csdn.net/caroline_wendy/article/details/22280461

環(huán)境: Android 0.5.2 + gradle 1.11 + kindle fire

Download, 下載項(xiàng)目, 從Internet上下載資源, 并存入本地SD卡.

點(diǎn)擊Download按鈕, 下載圖片, 然后顯示下載內(nèi)容, 可以點(diǎn)擊查看.

Download的具體設(shè)計(jì):

1. 修改activity_main.xml

位置: res->layout->activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:paddingBottom="@dimen/activity_vertical_margin"     tools:context="mzx.spike.download.app.MainActivity">      <Button         android:id="@+id/download_button"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Download"/>      <TextView         android:text="@string/hello_world"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/download_button" />  </RelativeLayout>

添加一個(gè)按鈕(Button), 使"hello_world"在其下方, 使用layout_below屬性;

2. 修改AndroidManifest.xml

位置: main->AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="mzx.spike.download.app" >      <uses-permission android:name="android.permission.INTERNET"/>     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>          <application         android:allowBackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/AppTheme" >         <activity             android:name="mzx.spike.download.app.MainActivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>  </manifest>

需要INTERNET, 連接互聯(lián)網(wǎng)的權(quán)限; WRITE_EXTERNAL_STORAGE, 寫入存儲(chǔ)器的權(quán)限;

3. 修改MainActivity.java

位置: java->package->MainActivity.java

package mzx.spike.download.app;  import android.app.Activity; import android.app.DownloadManager; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast;  import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection;   public class MainActivity extends Activity {      private static final String TAG = "Download";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Button buttonDownload = (Button)findViewById(R.id.download_button);         buttonDownload.setOnClickListener(new View.OnClickListener() {             public void onClick(View v) {                 myDownload();             }         });     }       @Override     public boolean onCreateOptionsMenu(Menu menu) {                  // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.main, menu);         return true;     }      @Override     public boolean onOptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button, so long         // as you specify a parent activity in AndroidManifest.xml.         int id = item.getItemId();         if (id == R.id.action_settings) {             return true;         }         return super.onOptionsItemSelected(item);     }      private void myDownload() {          String serviceString = Context.DOWNLOAD_SERVICE;         DownloadManager downloadManager;         downloadManager = (DownloadManager) getSystemService(serviceString);          Uri uri = Uri.parse("http://bizhi.zhuoku.com/2014/03/26/TAHITI/TAHITI02.jpg");         DownloadManager.Request request = new DownloadManager.Request(uri);          request.setDestinationInExternalPublicDir("/Download", "file.jpg");         request.setVisibleInDownloadsUi(true);         long reference = downloadManager.enqueue(request);          Log.d(TAG, "Download Reference: " + reference);          Cursor c = downloadManager.query(new DownloadManager.Query().setFilterById(reference));         if (c == null) {             Toast.makeText(this, "Download not found!", Toast.LENGTH_LONG).show();         } else {             c.moveToFirst();             Log.d(getClass().getName(), "Column_id : " +                     c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));             Log.d(getClass().getName(), "Column_bytes_downloaded so far : " +                     c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)));             Log.d(getClass().getName(), "Column last modified timestamp : " +                     c.getLong(c.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP)));             Log.d(getClass().getName(), "Column local uri : " +                     c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));             Log.d(getClass().getName(), "Column statue : " +                     c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)));             Log.d(getClass().getName(), "Column reason : " +                     c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)));              Toast.makeText(this, statusMessage(c), Toast.LENGTH_LONG).show();         }          startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));     }       private String statusMessage(Cursor c){         switch(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))){             case DownloadManager.STATUS_FAILED:                 return "Download failed";             case DownloadManager.STATUS_PAUSED:                 return "Download paused";             case DownloadManager.STATUS_PENDING:                 return "Download pending";             case DownloadManager.STATUS_RUNNING:                 return "Download in progress!";             case DownloadManager.STATUS_SUCCESSFUL:                 return "Download finished";             default:                 return "Unknown Information";         }     }  }

詳解:

1. 在創(chuàng)建界面中, 填寫按鈕(button)點(diǎn)擊事件(setOnClickListener), 觸發(fā)下載方法(myDownload);

2. DownloadManager 獲取下載的服務(wù)(Context.DOWNLOAD_SERVICE);

3. 設(shè)置下載的鏈接(uri), 設(shè)置存儲(chǔ)位置(setDestinationInExternalPublicDir), 顯示下載用戶接口(setVisibleInDownloadsUi);

4. 放置下載隊(duì)列, 開始下載, downloadManager.enqueue(request);

5. 通過游標(biāo)獲取下載信息, downloadManager.query();

6. 輸出相應(yīng)的下載信息(COLUMN_*), 和下載狀態(tài)(STATUS_*);

7. 顯示下載視圖(DownloadManager.ACTION_VIEW_DOWNLOADS);

注意:

存儲(chǔ)位置(setDestinationInExternalPublicDir), 需要提前設(shè)置, 否則無法查看文件, 存儲(chǔ)至content位置;

4. 運(yùn)行程序


下載地址: http://download.csdn.net/detail/u012515223/7106567

Android - Download(下載) 項(xiàng)目 詳解

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前名稱:Android-Download(下載)項(xiàng)目詳解-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article8/pdjop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、虛擬主機(jī)軟件開發(fā)、品牌網(wǎng)站設(shè)計(jì)自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)