【问题标题】:How do I use DownloadListener?如何使用下载监听器?
【发布时间】:2012-04-01 02:26:14
【问题描述】:

我正在创建一个应用程序,它允许大学生从应用程序而不是浏览器中下载他们的学习资料。 主页有很多主题名称。 每个主题名称都指向新网页。 所以,我使用了WebViewClient。 但是,在最后一页,当我点击 *.ppt 或 *.pdf 文件时,它会打开垃圾。

我希望在应用程序中下载这些文件。

如何实现DownloadListener

package jiit.app;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class sm extends Activity
{  
    WebView browser;

    protected void onCreate(Bundle anyvar) 
    {
        super.onCreate(anyvar);
        setContentView(R.layout.sm);
        browser=(WebView)findViewById(R.id.webkit);
        WebSettings webSettings = browser.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        browser.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        browser.setWebViewClient(new WebViewClient());
        {
            browser.loadUrl("http://www.sm.ividhya.com/j128/");
        }  
    }
}

【问题讨论】:

    标签: android webviewclient webclient


    【解决方案1】:

    试试这个DownloadListener 示例:

    public class webActivity  extends Activity {
    
     WebView webView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            this.webView=(WebView) this.findViewById(R.id.webview);
            this.webView.getSettings().setSupportZoom(false);
            this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            this.webView.loadUrl("http://www.sm.ividhya.com/j128/");
            this.webView.setWebViewClient(new WebViewClientDemo());
            webView.setDownloadListener(new DownloadListener() {
                public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimetype,
                                long contentLength) {
    
                                              Uri uri = Uri.parse(url);
               Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                        startActivity(intent);
                }
        });
    
        }
      private class WebViewClientDemo extends WebViewClient {
        @Override
    
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
          return true;
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      相关资源
      最近更新 更多