How to achieve interoperability between native and hybrid apps in production mode?

I'll start by explaining the usage I'm trying to implement. I have two different applications:

  • Native Android app and
  • Light-based hybrid application

The use case starts with opening a native Android app. On a specific event, I open a hybrid app with some parameters.

In a hybrid app, I receive the passed parameters on its native side, and now I want to use that data in the web app of the app (JavaScript, HTML). How can I achieve this?

For example:
I opened my first Android app. Who has one text box and a button. I entered my mobile number in the text box and clicked the button. On a button click, I have a code that launches another hybrid app and passes the mobile number with it.

I can extract this mobile number on my own side of the code. How do I pass this to a web part (JavaScript)?

Any help would be appreciated.

+3


source to share


2 answers


I will describe the solution using code snippets.

First, open your mashup from your native app.

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

      

Now the Worklight based hybrid app will start, and from the main body, we will extract the passed data and store it in the general settings:

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

      



Now create a plugin that you can call after the web part is ready to 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 this plugin on your Worklight mashup website.

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

      

+2


source


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

  • Use Simple Data Sharing API
    With this API, I don't think you even have to try to get data from your own view and bring it back to webview in your mashup, it will only be available in webview.

    Explaining the concept and execution in this answer would make it too long; I suggest going through the documentation first and see if it suits your needs.

    But I suggest:

  • Use the Action Sender API
    With this API, you can easily move data from the web to your home or home on the web.

    In your case, you are saying that you already have data in native code after you open the mashup and you only need to move it to webview, so this is necessary:

Unfortunately, there is currently no tutorial module to demonstrate exactly this feature, but there will be.



This is the basic premise of what you need to do:

  • In 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 mashup (or elsewhere, depending on your application), after the else

    closing bracket, do the following in onInitWebFrameworkComplete

    :

    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 is that after you open the application, you will be greeted with a warning with the message "12345".

+3


source







All Articles