How to achieve interaction between Native and Hybrid Applications in Worklight?

瘦欲@ 提交于 2019-12-12 16:56:15

问题


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.


回答1:


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:

    • Add a sender in the native code
    • Add a receiver in the JavaScript code

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".




回答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", []);


来源:https://stackoverflow.com/questions/25698289/how-to-achieve-interaction-between-native-and-hybrid-applications-in-worklight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!