首页 > 解决方案 > BrowserComponent "onDownloadStart" event

问题描述

I'm trying to build a simple web browser using BrowserComponent. Are there any options to check when a user clicks on a "download" button (how to detect download)? When developing directly with Android, there is an event "onDownloadStart". Is there something similar?

Thanks

标签: downloadcodenameone

解决方案


We don't support that behavior as it isn't portable. Androids download facility stores files "elsewhere" and requires some additional permissions. Instead you can intercept the URL navigation logic and decide whether you want to perform a download or not, you can then use something like the Util download methods to perform the actual file download.

e.g.:

bc.addBrowserNavigationCallback(url -> {
    // *** WARNING: this code runs off the EDT and must not block!!!! ***
    if(shouldIDownloadThisURL(url) {
        String file = getStorageFileNameForUrl(url);
        Util.downloadUrlToStorageInBackground(url, file, 
           ev -> fileDownloadCompleted(file));
        return false;
    }
    return true;
}); 

推荐阅读