从URL字符串在Fragment Webview中实现下载监听器

时间:2018-03-21 15:01:08

标签: java android android-fragments android-webview

我正在使用webView加载我从下一个片段传递的url作为String

String url1 = getArguments().getString("url1");

现在当网页加载到webView中时,我想添加下载监听器以使用外部浏览器下载。

我的片段java代码是

public class webb extends Fragment {

    WebView myWebView;
    private LinearLayout container;
    private Button nextButton, closeButton;
    private EditText findBox;
    ProgressBar pbar;

    public webb() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_webb, container,  false);



        String url1 = getArguments().getString("url1");


        myWebView=(WebView)rootView.findViewById(R.id.webview);
        pbar = (ProgressBar)rootView.findViewById(R.id.progressBar1);
        pbar.setVisibility(View.VISIBLE);

        myWebView.getSettings().setBuiltInZoomControls(true);

        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.getSettings().setLoadsImagesAutomatically(true);
        myWebView.getSettings().setJavaScriptEnabled(true);

        myWebView.getSettings().setBuiltInZoomControls(true);

        myWebView.setInitialScale(1);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);

        WebSettings webSettings = myWebView.getSettings();


        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl(url1);


        return rootView;


    }

    public static String changedHeaderHtml(String htmlText) {

        String head = "<head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head>";

        String closedTag = "</body></html>";
        String changeFontHtml = head + htmlText + closedTag;
        return changeFontHtml;

    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url1) {
            //remove your progressdialog here
            super.onPageFinished(view, url1);
            pbar.setVisibility(View.GONE);

        }

    }

    public boolean shouldOverrideUrlLoading(WebView view, String url1) {

        myWebView.loadUrl(url1);
        return true;


    }
}

有人建议我使用

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

        // download your stuff here

        return false;
    }

但是如何使用上面的解决方案在webView中加载字符串url1?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该在构建片段时将初始URL放在参数中(顺便说一句,您应该将其命名为Webb而不是webb):

public class webb extends Fragment {

    public static Webb createFragment(String url) {
        Webb webb = new Webb();
        Bundle arguments = new Bundle();
        arguments.putString("url1", url);
        webb.setArguments(arguments);
        return webb;
    }

}

如果您想在某个时间点打开外部浏览器,您可以:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { 

    if (!url.equals(getArguments().getString("url1"))) {
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);  
       return true;
    }

    return false;
}