我在设计一个应用程序时遇到了一个要求。
我的应用程序使用以下语法触发下载。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://openintents.googlecode.com/files/FileManager-2.0.2.apk"));
startActivity(intent);
以上代码打开浏览器页面,然后开始下载。
我的要求是我需要在下载完成后得到通知。
由于没有广播可以在浏览器完成下载时通知我,所以我使用了一些 hacky 方法。
我正在使用 RecursiveFileObserver
通过 FileObserver.CLOSE_WRITE
标志监视 SD 卡内容。
我的计划是调用此事件后,我会将路径内容与下载的文件名进行比较,然后触发下一步操作。
但我面临的问题是 FileObserver.CLOSE_WRITE 被多次调用,并且因设备而异。
我的问题是,是否有任何可用的 API 或解决方法可以在浏览器完成下载后通知我?
下面是我试过的代码 fragment 。
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();
}
最佳答案
您可以使用自己的服务执行下载,并在开始使用 getContentLength() 之前获取文件大小并跟踪下载的部分大小来跟踪进度。
关于android - 浏览器下载完成通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14041552/