我有一个 android 应用程序,它只是我的移动网站的包装。我有一个简单地指向我网站的登录 URL 的 webview。当我在常规浏览器上访问我的移动网站时,我可以在单击按钮时下载 PDF(通过 href="url 返回 pdf")。然而,在我的包装上,我无法下载它。我认为这与我的网络设置或我需要覆盖的某些方法有关。如何让我的包装器像普通浏览器一样下载文件?
代码:
public class MainActivity extends Activity {
private static final String TAG = "TAG";
private WebView myWebViewGlobal=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar as we already have it in the web app
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Point to the content view defined in XML
setContentView(R.layout.main);
//Configure the webview setup in the xml layout
WebView myWebView=(WebView) findViewById(R.id.webview);
myWebViewGlobal=myWebView;
WebSettings webSettings = myWebView.getSettings();
//Yes, we want javascript, pls.
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setDownloadListener(new DownloadListener()
{
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ECLAIR) {
try {
Log.d(TAG, "Enabling HTML5-Features");
Method m1 = WebSettings.class.getMethod("setDomStorageEnabled", new Class[]{Boolean.TYPE});
m1.invoke(webSettings, Boolean.TRUE);
Method m2 = WebSettings.class.getMethod("setDatabaseEnabled", new Class[]{Boolean.TYPE});
m2.invoke(webSettings, Boolean.TRUE);
Method m3 = WebSettings.class.getMethod("setDatabasePath", new Class[]{String.class});
m3.invoke(webSettings, "/data/data/" + getPackageName() + "/databases/");
Method m4 = WebSettings.class.getMethod("setAppCacheMaxSize", new Class[]{Long.TYPE});
m4.invoke(webSettings, 1024*1024*8);
Method m5 = WebSettings.class.getMethod("setAppCachePath", new Class[]{String.class});
m5.invoke(webSettings, "/data/data/" + getPackageName() + "/cache/");
Method m6 = WebSettings.class.getMethod("setAppCacheEnabled", new Class[]{Boolean.TYPE});
m6.invoke(webSettings, Boolean.TRUE);
Method m7 = WebSettings.class.getMethod("setAllowContentAccess", new Class[]{Boolean.TYPE});
m7.invoke(webSettings, Boolean.TRUE);
Method m8 = WebSettings.class.getMethod("setAllowFileAccessFromFileURLs", new Class[]{Boolean.TYPE});
m8.invoke(webSettings, Boolean.TRUE);
Method m9 = WebSettings.class.getMethod("setAllowUniversalAccessFromFileURLs", new Class[]{Boolean.TYPE});
m9.invoke(webSettings, Boolean.TRUE);
Method m10 = WebSettings.class.getMethod("setLoadsImagesAutomatically", new Class[]{Boolean.TYPE});
m10.invoke(webSettings, Boolean.TRUE);
Method m11 = WebSettings.class.getMethod("setLoadsImagesAutomatically", new Class[]{Boolean.TYPE});
m11.invoke(webSettings, Boolean.TRUE);
Log.d(TAG, "Enabled HTML5-Features");
}
catch (NoSuchMethodException e) {
Log.e(TAG, "Reflection fail", e);
}
catch (InvocationTargetException e) {
Log.e(TAG, "Reflection fail", e);
}
catch (IllegalAccessException e) {
Log.e(TAG, "Reflection fail", e);
}
}
//Make sure links in the webview is handled by the webview and not sent to a full browser
myWebView.setWebViewClient(new WebViewClient());
//And let the fun begin
myWebView.loadUrl("http://mywebsite.com/login.php");
}
@Override
public void onBackPressed()
{
if(myWebViewGlobal.canGoBack())
{
myWebViewGlobal.goBack();
}
return;
}
}