0

在 Gingerbread 中它工作正常。但在 jellybean 中它有时会启动 webbrowser,有时会在 WebView 中加载网页。我不知道为什么会发生这种情况。我在许多站点中检查了这个问题,当然在 stackoverflow 中,但我被撤消了。请帮助我。

我的代码是..

public class CityTouchFragment extends Fragment {
View v;
static WebView webView;
TextView tv;
public static int track = 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.webload, null);
    webView = (WebView) v.findViewById(R.id.load);
    tv = (TextView) v.findViewById(R.id.loading);

    ConnectionDetector connectionDetector = new ConnectionDetector(
            getActivity());

    if (connectionDetector.isConnectingToInternet() == true)

    {

        new DownloadTask()
                .execute("Any parameters my download task needs here");
    }

    else {
        Toast.makeText(getActivity(), "Check Internet Connrction",
                Toast.LENGTH_SHORT).show();
        FragmentTabHost tabHost = Tabactivty.self.mTabHost;

        tabHost.setCurrentTab(0);
    }
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.requestFocus(View.FOCUS_DOWN);
    webView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                v.requestFocusFromTouch();
                break;
            }
            return false;
        }

    });

    return v;
}

private class DownloadTask extends AsyncTask<String, Void, Object> {
    @Override
    protected void onPreExecute() {

    }

    protected Object doInBackground(String... args) {
        Log.i("MyApp", "Background thread starting");

        webView.getSettings().setJavaScriptEnabled(true);
        webView.requestFocus(View.FOCUS_DOWN);
        webView.loadUrl("http://www.citytouch.com.bd/mobile");

        webView.setWebViewClient(new MyWebViewClient());

        return "replace this with your data object";
    }

    protected void onPostExecute(Object result) {

        webView.requestFocus(View.FOCUS_DOWN);
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    v.requestFocusFromTouch();
                    break;
                }
                return false;
            }

        });

    }

}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        System.out.println("on finish");

        tv.setVisibility(View.GONE);

        track = 2;
    }
}

}

4

2 回答 2

0

据我所知,这段代码不应该打开 webBrowser。它应该在 webView 中加载所有数据。
要在 Web 浏览器中打开任何链接,请使用以下代码:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
于 2013-10-22T14:49:35.430 回答
0

放在webView.setWebViewClient(new WebViewClient());前面loadUrl("")

解析字符串http时:默认webClient打开浏览器。不知道为什么!!

于 2014-05-27T11:16:44.083 回答