1 つの要件に出くわすアプリケーションを設計しています。
私のアプリケーションは、次の構文を使用してダウンロードを開始しています。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://openintents.googlecode.com/files/FileManager-2.0.2.apk"));
startActivity(intent);
上記のコードはブラウザ ページを開き、ダウンロードを開始します。
私の要件は、ダウンロードが完了したら通知を受け取る必要があるということです。
ブラウザがダウンロードを完了したときに通知するブロードキャストが利用できないため、私はややハックなアプローチを使用しました。
SDカードの内容をフラグRecursiveFileObserverで監視するために使用しています。FileObserver.CLOSE_WRITE
私の計画は、このイベントが呼び出されたら、パスの内容をダウンロードしたファイル名と比較し、次のアクションをトリガーすることです。
しかし、私が直面している問題は、FileObserver.CLOSE_WRITE が複数回呼び出され、デバイスごとに異なることです。
私の質問は、ブラウザのダウンロードが完了すると通知される API または回避策はありますか?
以下は、私が試したコードスニペットです。
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();
}