在 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;
}
}
}