How can I wait until DownloadManager is complete?

后端 未结 2 1590
忘了有多久
忘了有多久 2021-01-15 09:59

This is the code I\'m currently using

                String iosjiUrl = \"http://modapps.com/NotoCoji.ttf\";
                DownloadManager.Request request          


        
相关标签:
2条回答
  • 2021-01-15 10:35

    A Broadcast intent action sent by the download manager when a download completes so you need to register a receiver for when the download is complete:
    
    To register receiver
    
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    
    and a BroadcastReciever handler
    
    BroadcastReceiver onComplete=new BroadcastReceiver() {
        public void onReceive(Context ctxt, Intent intent) {
            // your code
        }
    };
    
    You can also create AsyncTask to handle the downloading of big files
    
    Create a download dialog of some sort to display downloading in notification area and than handle the opening of the file:
    
    protected void openFile(String fileName) {
        Intent install = new Intent(Intent.ACTION_VIEW);
        install.setDataAndType(Uri.fromFile(new File(fileName)),"MIME-TYPE");
        startActivity(install);
    }
    
    you can also check the sample link
    sample

    0 讨论(0)
  • 2021-01-15 10:36

    Use a BroadcastReceiver to detect when the download finishes:

    public class DownloadBroadcastReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
    
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                //Show a notification
            }
        }
    }
    

    and register it in your manifest:

    <receiver android:name=".DownloadBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
        </intent-filter>
    </receiver>
    
    0 讨论(0)
提交回复
热议问题