[英]Android DownloadManager not working on smartphone
Currently I am working on displaying pdf with WebView and trying to implement a download function. 目前,我正在使用WebView显示pdf并尝试实现下载功能。 I am using download manager to do it. 我正在使用下载管理器来做到这一点。 It works on emulator where pdf file is downloaded. 它适用于下载了pdf文件的模拟器。 However, in smartphone, it just showed 'Download 1 task'. 但是,在智能手机中,它仅显示“下载1个任务”。 After few second, the notification gone without download anything. 几秒钟后,通知消失,没有下载任何内容。 I am implement it in Api 19 and above. 我将在Api 19及更高版本中实现它。 My smartphone is android 5.1. 我的智能手机是android 5.1。 Is there any solution on this problem? 这个问题有什么解决办法吗?
PdfFullscreenActivity.java PdfFullscreenActivity.java
public class PdfFullscreenActivity extends AppCompatActivity {
public WebView webview;
public String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_fullscreen);
Intent intent = getIntent();
str = intent.getExtras().getString("PDF_TAG");
String url = "https://docs.google.com/viewer?embedded=true&url=https://spmsejarahscore.000webhostapp.com/web/media/pdf/" + str;
webview = (WebView)findViewById(webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheMaxSize(1024 * 1024);
webview.getSettings().setAppCacheEnabled(true);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
}
public void onBackPressed(){
super.onBackPressed();
finish();
}
public void download(View v){
// https://spmsejarahscore.000webhostapp.com/web/media/pdf/SijilPelajaranMalaysia(SPM)_2011_P1,2.pdf
String downloadUrl = "https://spmsejarahscore.000webhostapp.com/web/media/pdf/" + str;
Uri urifile = Uri.parse(downloadUrl);
downloadfile(urifile, v);
}
public long downloadfile(Uri uri, View view){
long downloadReference;
DownloadManager downloadManager;
// Create request for android download manager
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Downloading");
//Setting description of request
request.setDescription(str);
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(PdfFullscreenActivity.this, Environment.DIRECTORY_DOWNLOADS, str);
//Enqueue download and save into referenceId
downloadReference = downloadManager.enqueue(request);
return downloadReference;
}
}
activity_pdf_fullscreen.xml activity_pdf_fullscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.foong.spmsejerahscore.PdfFullscreenActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:id="@+id/downloadbtn"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:onClick="download"
android:background="#264745"
android:src="@mipmap/ic_download" />
</RelativeLayout>
加:
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.