应用程序启动Web浏览器,用于加载网页,而不是在Android中的WebView中加载

时间:2013-10-22 14:11:01

标签: android webview

在姜饼中工作正常。但在软糖中它有时会启动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;
    }
}

}

2 个答案:

答案 0 :(得分:0)

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

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

答案 1 :(得分:0)

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

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