Search code examples
androiddownloadcallbackbroadcast

DownloadManager check if url is valid


I'm trying to download an .apk file from an URL using DownloadManager. When the URL is OK , everything works as expected but when I enter a wrong URL, I see that the download starts and very quickly stops but onReceive method never gets called.

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {

        if(command.equals(Protocol.APPS_UPDATE_ONROUTE_APP)) {
               IntentFilter screenStateFilter = new IntentFilter();
               my_context.registerReceiver(new MyBroadCastReciever(), screenStateFilter);
               CommandLine.execCommandLine("monkey -p com.fagica.flosko -c android.intent.category.LAUNCHER 1");
        }
    }
}

What could I do to detect that URL is not a correct apk link?


Solution

  • You can make sure of correct link by getting size of the file.

    In general, if you get exact size of file, it means there is a correct link. In case you have entered wrong link, your content length will be incorrect.

    You can get content length by,

    URL url = new URL("YOU_LINK_HERE");
    long size = url.openConnection().getContentLength();
    

    Required permission,

    <uses-permission android:name="android.permission.INTERNET" />