android - Show Download Manager progress inside activity -
android - Show Download Manager progress inside activity -
i used download manager class within activity perform downloads; works fine , next task show same progress percentage within activity. not sure how it.
my code far
public class downloadsamplebook extends activity{ private long enqueue; private downloadmanager dm; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_sample_download); broadcastreceiver receiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if (downloadmanager.action_download_complete.equals(action)) { long downloadid = intent.getlongextra( downloadmanager.extra_download_id, 0); query query = new query(); query.setfilterbyid(enqueue); cursor c = dm.query(query); if (c.movetofirst()) { int columnindex = c .getcolumnindex(downloadmanager.column_status); if (downloadmanager.status_successful == c .getint(columnindex)) { view.setimageuri(uri.parse(uristring)); } } } } }; registerreceiver(receiver, new intentfilter( downloadmanager.action_download_complete)); } public void onclick(view view) { dm = (downloadmanager) getsystemservice(download_service); request request = new request( uri.parse("http://abc.com/a.png")); enqueue = dm.enqueue(request); } public void showdownload(view view) { intent = new intent(); i.setaction(downloadmanager.action_view_downloads); startactivity(i); } } is there method give progress download percentage?
if looking decent way determine when query downloadmanager progress updates, consider registering contentobserver uri content://downloads/my_downloads
example:
downloadmanager manager = (downloadmanager) getsystemservice( context.download_service ); manager.enqueue( myrequest ); uri mydownloads = uri.parse( "content://downloads/my_downloads" ); getcontentresolver().registercontentobserver( mydownloads, true, new downloadobserver() ); ... public static class downloadobserver extends contentobserver { @override public void onchange( boolean selfchange, uri uri ) { log.d( "downloadobserver", "download " + uri + " updated" } this yields next output each chunk of long running download received
d/downloadobserver(15584): download content://downloads/my_downloads/437 updated d/downloadobserver(15584): download content://downloads/my_downloads/437 updated d/downloadobserver(15584): download content://downloads/my_downloads/437 updated d/downloadobserver(15584): download content://downloads/my_downloads/437 updated where '437' id of download.
note follows content uri defined in class android.provider.downloads appears hidden in framework , may not work consistently on devices. (https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/downloads.java#89)
android android-download-manager
Comments
Post a Comment