package com.webview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Webview_integrationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.setWebViewClient(new MyWebViewClient());
WebViewClient client = new MyWebViewClient()
{
@Override
public void launchExternalBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
};
}
}
***************************************************************************************
package com.webview;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public abstract class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.google.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
launchExternalBrowser(url);
return true;
}
public abstract void launchExternalBrowser(String url);
}
**当我运行这个程序时,我得到错误为“无法实例化MyWebViewClient类型”。我知道这是因为该类是抽象的。但是如何解决我的问题。我已经粘贴了这两个类的代码我正在使用。
答案 0 :(得分:0)
首先:您无法实例化抽象类。你只能扩展一个抽象类。
第二:创建自己的WebViewClient类(MyWebViewClient)需要什么。您可以在主活动中使用webViewClient覆盖shouldOverrideUrlLoading(WebView视图,String url)方法,并在此方法中使用launchExternalBrowser(String url)方法执行相同的操作。
我的意思是你在launchExternalBrowser(String url)方法中触发了一个intent,它是从shouldOverrideUrlLoading(WebView视图,String url)方法调用的。那你为什么不在shouldOverrideUrlLoading(WebView视图,字符串url)方法内部触发相同的Intent itselft。
答案 1 :(得分:-4)
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class Webview_integrationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView mywebview = (WebView) findViewById(R.id.webView1);
mywebview.setWebViewClient(new WebViewClient());
Button button1 = (Button) findViewById(R.id.My_btn);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(
"android.intent.action.VIEW",
Uri.parse("https://www.google.com")));
// TODO Auto-generated method stub
}
});
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.google.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}