简体   繁体   中英

Javascript-to-Java Communication in an Android Phonegap app

I am working on a Phonegap application that uses an Android service to check for message updates while the app isn't being used. In order to do that, however, I need the session key generated by the user's username and password. The generation of the session key is handled on the Javascript/HTML side of things while the app is active in the foreground.

My question is, how do I access a Javascript variable for use in Java? I need the session key value as a String object in Java. I just need to access it once, and I don't have a clue how Javascript-to-Java communication works.

You can pass a javascript variable to the java while using the plugins as below.

cordova.exec(null, null, "service", "action", ["firstArgument", "secondArgument", 42]);

Here the first and second parameter to the exec method are the sucess and failure calllback. service and action are the native class and method names respectively. And the last parameter ["firstArgument", "secondArgument", 42] are the javascript variables which you can pass to the native method.

For more details please go through the following link. http://docs.phonegap.com/en/2.8.0/guide_plugin-development_index.md.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM