These days with the NEXUS5 4.4.4 system to do a simple mobile phone device number to get, and then invoke JavaScript display in the Web page function, has done more than n similar programs, the results of the program a run any problems are out, hehe
[Info:console (1)] "Uncaught Referenceerror:is not define
I/chromium (490): [Info:console (1)] "uncaught syntaxerror:unexpected token
[Android] Web console:uncaught Typeerror:object [Object Object] has no method ' xxx '
The conclusion is that the above problem, first look at the code
Demo.html
Take a look at my Android code as follows:
private void Loadurl () {
String key= "";
String androidid= "";
try{
androidid = secure.getstring (Getcontentresolver (), secure.android_id);
LOG.D (TAG, "Androidid:" +androidid);
} catch (Exception e) {
log.e (TAG, "");
} finally{
String Script=string.format ("Javascript:getdeviceid (' +androidid+ ')");
Mwebview.evaluatejavascript (script, New valuecallback<string> () {
@Override public
Void Onreceivevalue (String value) {
log.d (TAG, "Onreceivevalue value=" + value);
if (value!=null) {flag_get_deviceid=true}}}
);
Mwebview.loadurl ("Javascript:getdeviceid (' Maomao ')");
}
When the Loadurl method is in the OnCreate method of the activity, Mwebview.loadurl ("file:///android_asset/demo.html"); Call Loadurl after execution ();
Basically the above error, I think is not 4.4 of the system in writing with a low version of the same, or the use of 2 ways to invoke:
Mwebview.evaluatejavascript
Mwebview.loadurl ("Javascript:getdeviceid (' Maomao ')");
The final judgment is one possibility: when calling the Getdeviceid method, JS is not loaded.
Solution :
Private class Webviewclientdemo extends Webviewclient {
@Override public
boolean shouldoverrideurlloading ( WebView view, String URL) {
log.d (TAG, "URL:" +url);
View.loadurl (URL);//When a new link is opened, the current webview is used and no other browser of the system is used to return
true;
@Override public
void onpagefinished (webview view, String URL) {
super.onpagefinished (view, URL);
Execute the JS function
if (!flag_get_deviceid) {
loadurl ()}
} that you want to call here
We are in the onpagefinished, all of the JS must be loaded, and then the implementation of Loadurl method, it can be.
Finally, incidentally, the benefits of Webview.evaluatejavascript, this method, you can get the JS function return results to:
Mwebview.evaluatejavascript (script, New valuecallback<string> () {
@Override public
Void Onreceivevalue (String value) {
log.d (TAG, "Onreceivevalue value=" + value);
if (value!=null) {flag_get_deviceid=true}}}
);
Join CSDN Technology Group: 221057495 Communicate with me