**请参见下面的问题**以解决问题**
确定一切正常,除了加载网址,当它尝试将网址加载到网页视图中时,它会在超链接前面添加网址:例如:url:http://google.com并且webview当然不会加载它。我试过.replace(" url:","")和.replaceAll(" url:","")没有成功。使用zbar条形码库生成SCAN_RESULT。
主要活动
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ZBAR_SCANNER_REQUEST:
case ZBAR_QR_SCANNER_REQUEST:
if (resultCode == RESULT_OK) {
String value = data.getStringExtra(ZBarConstants.SCAN_RESULT);
String newValue = value.replaceAll("url:", "");
Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
intent.putExtra("mUrl",newValue);
startActivity(intent);
//Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_LONG).show();
} else if(resultCode == RESULT_CANCELED && data != null) {
String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
if(!TextUtils.isEmpty(error)) {
Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
}
}
break;
}
}
Webview活动
public class WebViewActivity extends Activity {
private WebView mWebView;
@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String mUrl = getIntent().getStringExtra("mUrl");
//String newMUrl = mUrl.replaceAll("url:", "");
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebViewClient(new MyAppWebViewClient());
}
WebView客户端
public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
}
MainActivity(已修复)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ZBAR_SCANNER_REQUEST:
case ZBAR_QR_SCANNER_REQUEST:
if (resultCode == RESULT_OK) {
String value = data.getStringExtra(ZBarConstants.SCAN_RESULT);
//The FIX IS BELOW url is case-sensitive, instead of url: it's URL:
String newValue = value.replace("URL:", "");
Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
intent.putExtra("mUrl",newValue);
startActivity(intent);
//Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_LONG).show();
} else if(resultCode == RESULT_CANCELED && data != null) {
String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
if(!TextUtils.isEmpty(error)) {
Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
}
}
break;
}
}
答案 0 :(得分:0)
我认为您不必将以下代码放在 shouldOverrideUrlLoading 中,因为返回true告诉WebView,URL将由以下代码处理。即WebView根本不会加载URL。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
因此,如果您希望在网页浏览中打开网址,只需在 shouldOverrideUrlLoading 中调用 return false ,除非您要启动其他应用(如Android股票浏览器) )打开网址。