admin管理员组文章数量:1357405
I am using the following code in the main activity , its giving the function display() is not defined
public class cordovaExample extends DroidGap {
Context mcontext;
private novel n;
private Server s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Thread(new Server(this)).start();
try {
Thread.sleep(500);
} catch (InterruptedException e) { }
new Thread(new Client()).start();
super.init();
n = new novel(this, appView);
s = new Server(this,appView);
appView.getSettings().setJavaScriptEnabled(true);
appView.addJavascriptInterface(n, "novel");
appView.addJavascriptInterface(s, "Server");
super.loadUrl("file:///android_asset/www/index.html");
super.loadUrl("javascript:display()");
}
}
At the last line it giving the error display() is not defined
function display() {
alert("abc");
}
Above code shows the display function which is I am using in the html file
Any type of help will be appreciated
I am using the following code in the main activity , its giving the function display() is not defined
public class cordovaExample extends DroidGap {
Context mcontext;
private novel n;
private Server s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Thread(new Server(this)).start();
try {
Thread.sleep(500);
} catch (InterruptedException e) { }
new Thread(new Client()).start();
super.init();
n = new novel(this, appView);
s = new Server(this,appView);
appView.getSettings().setJavaScriptEnabled(true);
appView.addJavascriptInterface(n, "novel");
appView.addJavascriptInterface(s, "Server");
super.loadUrl("file:///android_asset/www/index.html");
super.loadUrl("javascript:display()");
}
}
At the last line it giving the error display() is not defined
function display() {
alert("abc");
}
Above code shows the display function which is I am using in the html file
Any type of help will be appreciated
Share Improve this question edited Jul 20, 2013 at 9:48 stealthjong 11.1k14 gold badges48 silver badges86 bronze badges asked Sep 20, 2012 at 13:47 akshay1728akshay1728 3832 gold badges6 silver badges16 bronze badges2 Answers
Reset to default 6It's a bad idea to make Cordova load JavaScript on page load. This should be handled by your local JavaScript. Try to call your display() function like this in the HTML page itself:
<script>
function display()
{
alert("abc");
}
window.onload = function() {
display();
}
</script>
If you need to call a JavaScript from within Cordova at any later point, it is possible to do so this way:
sendJavascript("display();");
To access this method from other classes, you will need to access your main activity. The easy-but-perhaps-unsafe method is to create a static variable in your main Activity that will hold the activity itself. Example:
public class MyActivity extends DroidGap {
public static MyActivity activity;
public void onCreate(Bundle savedInstanceState) {
activity = this;
}
}
Then, from anywhere in your classes, do:
MyActivity.activity.sendJavascript('display();');
From Cordova 2.6 you can override onMessage in your CordovaActivity (DroidGap), you have to capture the message "onPageFinished", then you can call any function declared on the document:
@Override
public Object onMessage(String id, Object data) {
if("onPageFinished".equals(id)) {
sendJavascript("display('abc');");
}
return super.onMessage(id, data);
}
And in the HTML:
<script>
function display(arg) {
alert(arg);
}
</script>
Other option is to call it in the onResume() function of the CordovaActivity:
@Override
public void onResume() {
super.onResume();
sendJavascript("display('abc');");
}
本文标签: Calling javascript function from the android activityStack Overflow
版权声明:本文标题:Calling javascript function from the android activity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744047823a2581860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论