Android DownloadManager Example

<p>main.xml</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;&gt; &lt;Buttonandroid:text=&quot;StartDownload&quot;android:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; android:onClick=&quot;onClick&quot;&gt;&lt;/Button&gt; &lt;Buttonandroid:text=&quot;ViewDownloads&quot;android:id=&quot;@+id/button2&quot; android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; android:onClick=&quot;showDownload&quot;&gt;&lt;/Button&gt; &lt;ImageViewandroid:layout_height=&quot;wrap_content&quot;android:id=&quot;@+id/imageView1&quot; android:src=&quot;@drawable/icon&quot;android:layout_width=&quot;wrap_content&quot;&gt;&lt;/ImageView&gt; &lt;/LinearLayout&gt;</pre><pre class="brush:java;toolbar:false">packagede.vogella.android.downloadmanager; importandroid.app.Activity; importandroid.app.DownloadManager; importandroid.app.DownloadManager.Query; importandroid.app.DownloadManager.Request; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.database.Cursor; importandroid.net.Uri; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.ImageView; publicclassDownloadManagerActivityextendsActivity{ privatelongenqueue; privateDownloadManagerdm; /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); BroadcastReceiverreceiver=newBroadcastReceiver(){ @Override publicvoidonReceive(Contextcontext,Intentintent){ Stringaction=intent.getAction(); if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){ longdownloadId=intent.getLongExtra( DownloadManager.EXTRA_DOWNLOAD_ID,0); Queryquery=newQuery(); query.setFilterById(enqueue); Cursorc=dm.query(query); if(c.moveToFirst()){ intcolumnIndex=c .getColumnIndex(DownloadManager.COLUMN_STATUS); if(DownloadManager.STATUS_SUCCESSFUL==c .getInt(columnIndex)){ ImageViewview=(ImageView)findViewById(R.id.imageView1); StringuriString=c .getString(c .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); view.setImageURI(Uri.parse(uriString)); } } } } }; registerReceiver(receiver,newIntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } publicvoidonClick(Viewview){ dm=(DownloadManager)getSystemService(DOWNLOAD_SERVICE); Requestrequest=newRequest( Uri.parse(&quot;http://www.vogella.de/img/lars/LarsVogelArticle7.png&quot;)); enqueue=dm.enqueue(request); } publicvoidshowDownload(Viewview){ Intenti=newIntent(); i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); startActivity(i); } }</pre><p>permission设置</p>
RangeTime:0.007266s
RangeMem:206.58 KB
返回顶部 留言