Android包装器不会下载文件

时间:2012-12-03 20:57:18

标签: android webview wrapper android-websettings

我有一个Android应用程序,它只是我的移动网站的包装。我有一个webview只是指向我的网站的登录URL。当我在常规浏览器上访问我的移动网站时,我可以在单击按钮时下载PDF(通过href =“返回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;  
}  

}

0 个答案:

没有答案