开发者

Android WebView Download Listener

Did anyone know how to get the full url to the content that should be downloaded in webview. I notice there is setDownloadListener (DownloadListener listener) method where we can get the url onDownloadStart method.

But i have problem when i open page A, i click hyperlink in page A and directly open a new page B. In page B i download some files so i want to know the full u开发者_Python百科rl of the file i just download, but i can't use the method above. Any solution??

Thanks.


I have solved same problem in android eclipse, first i have created a webview then added downloadListner, after that i am checking either machine is connected to the internet or not, if so, the URL is loaded into webview, else an error message appears "No Network", That's it. You can check the code below which i am using in my app.

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView wv = (WebView) findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient());
        wv.setDownloadListener(new DownloadListener()
        {
            public void onDownloadStart(String url, String userAgent,StringcontentDisposition, String mimetype,long contentLength) 
            {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);   
            }
        });
        ConnectivityManager connMgr = (ConnectivityManager)
        this.getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi =
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile =
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if( wifi.isAvailable() )
        {
            wv.loadUrl("http://www.example.com");
        }
        else if( mobile.isAvailable() )
        {
            wv.loadUrl("http://www.example.com");
        }
        else
        {
            Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
        }        
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜