my task follows: using ibm mobilefirst create hybrid app , implement js calculator. show date retrieved native java apis web page.
my attempts:
- i followed documentations here , implemented whole native code oncreate method
i found this answer"the first one" illustrating should use on oninitwebframeworkcomplete,
- solution provided didn't work
- i working mobilefirst version 7
- full sample code provided
suggestion: should create whole action bar in native code merge in cross ui, available? need send petite string of date
i not clear on attempts, here quick demonstration how click button in html , trigger send action api current date in java , return javascript, , display it.
index.html
<button onclick="getdatefromjava();">show current date java</button> main.js
function wlcommoninit(){ wl.app.addactionreceiver ("returneddatefromjava", returneddatefromjava); } function getdatefromjava() { wl.app.sendactiontonative("retrievedate"); } function returneddatefromjava(received){ if (received.action === "returneddatefromjava"){ alert (json.stringify(received)); } } main java class file
- find
oninitwebframeworkcomplete add
actionreceiverafterelse:import com.worklight.androidgap.api.wlactionreceiver; ... ... public void oninitwebframeworkcomplete(wlinitwebframeworkresult result){ if (result.getstatuscode() == wlinitwebframeworkresult.success) { super.loadurl(wl.getinstance().getmainhtmlfilepath()); } else { handlewebframeworkinitfailure(result); } actionreceiver actionreceiver = new actionreceiver(); wl.getinstance().addactionreceiver(actionreceiver); }
actionreceiver class
package com.getdateapp; import java.util.date; import org.json.jsonexception; import org.json.jsonobject; import com.worklight.androidgap.api.wl; import com.worklight.androidgap.api.wlactionreceiver; public class actionreceiver implements wlactionreceiver{ public void onactionreceived(string action, jsonobject data){ if (action.equals("retrievedate")){ date date = new date(); jsonobject returneddate = new jsonobject(); try { returneddate.put("datefromjava", date); } catch (jsonexception e) { e.printstacktrace(); } wl.getinstance().sendactiontojs("returneddatefromjava", returneddate); } } }
Comments
Post a Comment