5
私はwebviewからファイルをダウンロードしています。しかし、DownloadManagerを使用している場合、thisに類似した進行中のダウンロード通知は表示されません。バックグラウンド操作でやっているだけです。 ダウンロード中にステータスバーの通知を表示する方法。 ダウンロードしたファイルを取得できますが、通知中の進行中のプロセスを表示する方法はありますか?進捗状況シークバー通知がDownloadManagerで表示されないandroid
「request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);」を使用しました。それは進行中のプロセスを表示していません。私がやっている間違いを私に教えてください。
ここで私が使用したコードです。
mWebview.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType("pdf");
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
"pdf"));
request.allowScanningByMediaScanner();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//clueless why it's not working..
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
} else {
request.setShowRunningNotification(true);
}
//request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, "pdf"));
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadReference = dm.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, filter);
}
});
}
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadReference == referenceId) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
Cursor c = dm.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// process download
String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + title);//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
//Intent testIntent = new Intent("com.adobe.reader");
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
//testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
try {
startActivity(testIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, "No application available to view PDF",
Toast.LENGTH_SHORT).show();
}
// get other required data by changing the constant passed to getColumnIndex
}
}
}
}
};
Android 7.1.1で確認しました。
いただきましたあなたがダウンロードしようとするファイルのサイズ? – Mani
ファイルサイズは1Mbです@Mani – Shadow
1GBのファイルサイズで試しても、通知が表示されないことを確認できますか?私の疑わしいのはファイルサイズが1MBなので、すでに – Mani