在Android WebView中显示Javascript小部件
嘿,你在这儿 我试图创建一个应用程序,使用WebView来显示JavaScript小部件,但我似乎遇到了一个根本无法显示小部件的问题。我已经做了大量的研究,我认为我正在尝试做的是可能的 这是我的主要活动在Android WebView中显示Javascript小部件,javascript,android,webview,android-webview,Javascript,Android,Webview,Android Webview,嘿,你在这儿 我试图创建一个应用程序,使用WebView来显示JavaScript小部件,但我似乎遇到了一个根本无法显示小部件的问题。我已经做了大量的研究,我认为我正在尝试做的是可能的 这是我的主要活动 public class WebViewAppTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle saved
public class WebViewAppTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//grab the webview
WebView webview = (WebView) findViewById(R.id.webview);
//grab the webview settings
WebSettings websettings = webview.getSettings();
//enable javascript
websettings.setJavaScriptEnabled(true);
//add a js interface to display the widgets
webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");
//load a webpage
webview.loadUrl("file:///android_asset/javascript/index.html");
}
}
它加载文件index.html,如下所示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>WebView test!</title>
</head>
<body>
<h1>MyHTML</h1>
<div id="panel">
<section class="episode"></section>
</div>
<script src="https://sourceurl1.js" />
<script src="https://sourceurl2.js" />
<script src="https://sourceurl3.js" />
<script src="https://sourceurl4.js" />
<script src="./widget.js" />
<script src="./top-episodes.js" />
<script>
var TopEpisodes = new Company.TopEpisodes({
$element: $('#panel section.episodes')
});
TopEpisodes.init({
mode : 'realtime',
range : { from: 1341911040, to: 1341907200 },
path : ["4FB159EB613033AA710006DF"],
isEpisode : true
});
</script>
</html>
</body>
</html>
网络视图测试!
MyHTML
var TopSpices=新公司。TopSpices({
$element:$(“#面板部分.集”)
});
top.init({
模式:“实时”,
范围:{从:1341911040到:1341907200},
路径:[“4FB159EB613033AA710006DF”],
伊塞皮索:是的
});
当我测试我的应用程序(Nexus S 4G ICS 4.0.4)时,唯一显示的是一个空白的白色屏幕上的MyHTML标题。在xml中启用了Internet访问
我是否需要为WebView设置启用其他设置,或者我的HTML文件是否确实有问题?我是否需要在JS接口中使用addJavaScriptInterface方法/执行某些操作
不管怎样,我很固执,愿意接受任何建议!谢谢你抽出时间
编辑已有几天没有人回答这个问题了,如果我能做些什么来澄清这个问题或任何其他细节,请告诉我!或者如果可能的话。谢谢大家! 您的HTML错误,
您没有正确地指向该位置,<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>WebView test!</title>
</head>
<body>
<h1>MyHTML</h1>
<div id="panel">
<section class="episode"></section>
</div>
<!-- Below is the code if the js in the same location of your index.html file. -->
<script src="sourceurl1.js" />
<script src="sourceurl2.js" />
<script src="sourceurl3.js" />
<script src="sourceurl4.js" />
<!-- Below is the code if the js is outside the location of your index.html file. -->
<script src="../widget.js" />
<!-- Below is the code if the js is outside the location of your index.html file., inside folder js-->
<script src="../js/top-episodes.js" />
<script>
var TopEpisodes = new Company.TopEpisodes({
$element: $('#panel section.episodes')
});
TopEpisodes.init({
mode : 'realtime',
range : { from: 1341911040, to: 1341907200 },
path : ["4FB159EB613033AA710006DF"],
isEpisode : true
});
</script>
</html>
网络视图测试!
MyHTML
var TopSpices=新公司。TopSpices({
$element:$(“#面板部分.集”)
});
top.init({
模式:“实时”,
范围:{从:1341911040到:1341907200},
路径:[“4FB159EB613033AA710006DF”],
伊塞皮索:是的
});
为了给您提供更多帮助,请指定文件的位置,因此,由于缺乏HTML和Javascript方面的经验,我决定使用不同的应用程序 相反,我将使用这个图表库用Java本机开发这个小部件:然后使用HttpClient更新数据,而不是在Javascript的WebView中创建数据,因为我认为对于Android应用程序来说,这个解决方案将更简单、更快
谢谢大家抽出时间 你能在本地运行JS加载html文件吗file:///..." ? 通常不可能有安全限制(请参阅Chrome),因为它允许我加载html,我是从Android开发者网站的WebView示例中得到这个想法的。谢谢你的回复!看看我的答案。谢谢你的帮助!我决定走另一个方向(见答案),但你的时间是+1!