3

I will start with explaining the use case I am trying to implement. I have two different applications:

  1. Native android application, and
  2. Worklight-based hybrid application

The use case starts with opening native android application. On a particular event I am opening the Hybrid application with some parameters.

In the Hybrid application, I am getting the passed parameters in the native side it, and now I want to use that data in the webview of the application (JavaScript, HTML). How can I achieve that?

For example:
I opened the first android application. Which have one text box and a button. I entered my mobile number in text box and hit the button. On the button click I have code which starts the other hybrid application and passes the mobile number with it.

I am able to extract that mobile number parameter on native side of code. How to pass that to the Web (JavaScript) part of it?

Any help will be appreciated.

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
yogesh
  • 574
  • 6
  • 24

2 Answers2

3

If you are using Worklight 6.2, you can achieve this in 2 ways.

  1. Use the Simple Data Sharing API
    With this API I don't think you'll even need to try to get the data from the native view and move it back to the webview in your Hybrid app, it will just be available in the webview.

    Explaining the concept and execution in this answer will make it too long; I suggest to first review the documentation and see whether it fits your needs.

    But I suggest:

  2. Use the Action Sender API
    With this API you can easily move data from web to native or native to web.

    In your case, you say you already have the data in the native code after opening the Hybrid application, and you only need to move it to the webview, so what that is required is to:

Unfortunately at this time there is no training module available to demonstrate specifically this feature, but there will be.

This is the basic premise for what you'll need to do:

  • In the JavaScript you implement a receiver:

    function wlCommonInit(){
        WL.App.addActionReceiver ("doSomething", actionReceiver);  
    }
    
    function actionReceiver(received){
        // Do something with the received data.
        alert (received.data.someProperty);
    }
    
  • In the main Java class of the Hybrid application (or elsewhere, depending on your application) you implement the following in onInitWebFrameworkComplete after the else closing bracket:

    public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result){
        ...
        ...     
        else {
            handleWebFrameworkInitFailure(result);
        }
    
        JSONObject data = new JSONObject();
        try {
            data.put("someProperty", 12345);
        } catch (JSONException e) {
            // handle it...
        }
        WL.getInstance().sendActionToJS("doSomething", data);
    }
    

The end result would be that once you open the app, you'll be welcomed with an alert showing "12345".

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • We are using worklight 6.1, So can i apply these two ways suggested by you? – yogesh Sep 08 '14 at 06:38
  • Next time mention your Worklight version while asking a question. No, you cannot use these in 6.1 as they are new features in 6.2; you can instead look at the WL.NativePage.show API. Search the IBM Worklight 6.1 Knowledge Center: http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.1.0/wl_welcome.html -- also, upgrade to 6.2 – Idan Adar Sep 08 '14 at 06:42
  • Sorry for not mentioning the worklight version before before. As far as I know WL.NativePage.show is used to call a native library. How it will help me to achieve data sharing from native code to hybrid code? – yogesh Sep 08 '14 at 06:47
  • You don't call a "library" with this API. You need to read the documentation regarding it (link above) as well as review the training module (search for "Adding native functionality"). With this API you can also pass data. You might need to adjust your application accordingly if you want to solve your problem: http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.1.0/com.ibm.worklight.getstart.doc/start/c_gettingstarted.html – Idan Adar Sep 08 '14 at 06:50
  • I used shared preferences of android and made it work. Thank you for your help anyway. :) – yogesh Sep 08 '14 at 10:08
  • @yogesh, if this answer helped you, can you mark it as resolved? OR provide your solution in a separate answer so others could benefit from it. – Idan Adar Oct 15 '14 at 03:45
2

I will describe the solution using code snippets.

First Open the hybrid application from a native application.

Intent intent = getPackageManager().getLaunchIntentForPackage(“URI Of Target Application”);
intent.putExtra("someData", someData);
startActivity(intent);

Now Worklight based hybrid application will start and from native part we will extract that passed data and store it in shared preferences:

Bundle dataBundle = getIntent().getExtras();
String someData = dataBundle.getString("someData");
sharedpreferences = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
sharedpreferences.edit().putString("someData", someData);
sharedpreferences.commit();

Now make a plugin which you can call after the web part is ready for use.

SharedPreferences sharedpreferences = cordova.getActivity().getSharedPreferences(MyPREFERENCES,cordova.getActivity().MODE_PRIVATE);
if(sharedpreferences!=null) {
     String param = sharedpreferences.getString("someData", "-1");
     sharedpreferences.edit().remove("someData").commit();
     callbackContext.success(param); 
}

Call that plugin on web side of Worklight based hybrid application.

function onSuccessSharedData (param) {
     Param is the passed parameter
} 
Cordova.exec(onSuccessSharedData, onFailure, "pluginName", "action", []);
yogesh
  • 574
  • 6
  • 24