安卓:MobileFirst从本机发送数据到跨页 [英] Android: MobileFirst sending data from Native to cross page

查看:494
本文介绍了安卓:MobileFirst从本机发送数据到跨页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是:使用IBM MobileFirst创建一个混合的应用程序和实施JS计算器。显示日期从本地的Java API检索到的网页。

My Task is as follows: using IBM MobileFirst create a Hybrid app and implement a JS calculator. show date retrieved from native java APIs to the web page.

我尝试:


  1. 我跟之证件<一个href=\"http://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.dev.doc/devref/t_sending_actions_native_to_js.html\"相对=nofollow>此处并实现整个本土code onCreate方法

  2. 我发现<一个href=\"https://stackoverflow.com/questions/25698289/how-to-achieve-interaction-between-native-and-hybrid-applications-in-worklight/26689642#26689642\">this回答第一个说明我应该在onInitWebFrameworkComplete使用它,

  1. I followed Documentations here and implemented the whole Native code onCreate method
  2. I found this answer"the first one" illustrating that i should use it on onInitWebFrameworkComplete,


  • 提供解决方案没有奏效

  • 我与MobileFirst版本工作7

  • 全样本code是提供

  • Solution provided didn't work
  • I am working with MobileFirst version 7
  • full sample code is provided

建议:我应该创建本地code整个操作栏,然后在交界面进行合并,是可用?我只需要发送日期的娇小串

Suggestion: should i create the whole action bar in native code then merge it in the cross ui, is that available? I only need to send a petite string of date

推荐答案

我不是你的企图清楚,所以这里是一个快速的示范了如何在HTML中点击一个按钮,触发发送操作API来获取当前日期Java和它返回的JavaScript,然后显示出来。

I am not clear on your attempts, so here is a quick demonstration how to click a button in HTML and trigger the Send Action API to get the current Date in Java and return it to JavaScript, and then display it.

的index.html

<button onclick="getDateFromJava();">show current date from 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));
    }
}

主Java类文件


  1. 找到 onInitWebFrameworkComplete

  2. 添加 ActionReceiver 其他

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类

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);
        }
    }
}

这篇关于安卓:MobileFirst从本机发送数据到跨页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆