Pergunta

I am designing an application where I am stumble across one requirement.

My application is firing download by using following syntax.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://openintents.googlecode.com/files/FileManager-2.0.2.apk"));

startActivity(intent);

above code opens browser page which in turn starts download.

My requirement is I need to get notified once that download is complete.

Since there are no broadcast available which will notify me when browser completes download, I have used somewhat hacky approach.

I am using RecursiveFileObserver to monitor SD Card contents with FileObserver.CLOSE_WRITE flag.

My plan is once this event is called I will compare the path contents with downloaded file name and will trigger next action.

But the issue I am facing is FileObserver.CLOSE_WRITE is called multiple times and it varies device to device.

My question is, is there any API or workaround available which will notify me once browser completes download?

Below is code snippet that I have tried.

Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://openintents.googlecode.com/files/FileManager-2.0.2.apk"));

startActivity(intent);

myObserver = new RecursiveFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath()) {

    @Override
    public void onEvent(int event, String path) {

        if (event == FileObserver.CLOSE_WRITE) {

            if (path.indexOf("FileManager") != -1) {
                Log.i("vipul", "CLOSE_WRITE: " + path);

                /*
                 * Intent intent = new Intent(Intent.ACTION_VIEW);
                 * intent.setDataAndType(Uri.fromFile(new File(path)),
                 * "application/vnd.android.package-archive");
                 * startActivity(intent);
                 */

            }

        }
    }

};

myObserver.startWatching();

}

@Override
protected void onDestroy() {

    if (installReceiver != null) {
        myObserver.stopWatching();
        unregisterReceiver(installReceiver);
        installReceiver = null;

    }
    super.onDestroy();
}

Nenhuma solução correta

Outras dicas

You can perform the download using your own service and tracking the progress by fetching the file size before you start using getContentLength() and tracking the downloaded part size.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top