如何创建一个简单的Android浏览器?
我只是想创建一个 apk,它将接受一个 url,然后打开一个窗口,然后像浏览器一样运行,
到目前为止我已经:
public class Browser extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
这工作正常,除了当我打开一个链接时,它会将我带到实际的浏览器,我'我无法在哪里放置此代码来覆盖在新浏览器中打开的链接:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
I simply want to create an apk that will take a url, and open a window and simply run like a browser
so far I have:
public class Browser extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
This works fine, except when i open a link it'll take me to the actual browser, I'm having trouble where to place this code to override links opening in a new browser:
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
WebView 有一个
setWebViewClient
方法。所以你会做类似的事情
WebView has a
setWebViewClient
method.so you'd do something like
查看我的名为 FriarFramework 的项目,它是一个电子书应用程序发布商。
它基本上在本地获取 HTML 文件的集合并将它们打包到 WebView 中。
https://github.com/hanchang/Friar-Framework
Check out my project called FriarFramework, which is an ebook app publisher.
It basically takes a collection of HTML files locally and packages them up into a WebView.
https://github.com/hanchang/Friar-Framework
试试这个
Try this
这应该足够了。
有关更多信息,请参阅有关 在 WebView 中构建 Web 应用
This should be enough.
for more see the official doc about Building web apps in WebView
您必须实现 WebViewClient。
您可以在 shouldOverrideUrlLoading() 方法中检测 URL:
browser.setWebViewClient(new WebViewClient() {
@覆盖
公共布尔shouldOverrideUrlLoading(WebView视图,字符串url){
// 这里可以有 URL
}
});
You have to implement WebViewClient.
You can detect URL inside shouldOverrideUrlLoading() method:
browser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// You can have URL here
}
});