简体   繁体   English

Cordova(Android插件)-将数据​​从主要活动发送到InAppBrowser

[英]Cordova (Android Plugin) - Send data from Main Activity to InAppBrowser

I am using the Socket Mobile Capture SDK, which provides an easy method for receiving data from bluetooth connected barcode scanners assuming you can add a method to an android Activity class. 我正在使用Socket Mobile Capture SDK,假设您可以将方法添加到android Activity类中,那么它提供了一种从蓝牙连接的条形码扫描仪接收数据的简便方法。

Below is the MainActivity code I have created. 以下是我创建的MainActivity代码。 The onData method correctly fires each time an external bluetooth scanner is used. 每次使用外部蓝牙扫描仪时,onData方法都会正确触发。 I would like to forward this information to the inAppBrowser. 我想将此信息转发给inAppBrowser。 Is this possible or is there a better way to do this? 这可能吗,还是有更好的方法呢?

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // enable Cordova apps to be started in the background
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
            moveTaskToBack(true);
        }

        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);

        Capture.builder(getApplicationContext())
        .enableLogging(BuildConfig.DEBUG)
        .build();        
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onData(DataEvent event) {
        System.out.println("onData fired from MainActivity");
        DeviceClient device = event.getDevice();
        String data = event.getData().getString();
        System.out.println(data);
    }
}

Here is my code from index.js which sets up the InAppBrowser: 这是我来自index.js的代码,该代码设置了InAppBrowser:

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    onDeviceReady: function() {        
        this.receivedEvent('deviceready');
        alert('device ready');

        var url = environment == 'Development' ? developmentUrl : productionUrl;        
        inAppBrowserRef = cordova.InAppBrowser.open(url, '_blank', 'location=no'); //open the in app browser with no location bar
        inAppBrowserRef.addEventListener( "loadstop", function() { //Fired when browser is finished loading
            alert('inappbrowser loaded');          
        });        
    },    
    // Once the InAppBrowser finishes loading
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

app.initialize();

I was able to get this to work by using loadUrl("javascript:onData(\\""+data+"\\")"). 我能够通过使用loadUrl(“ javascript:onData(\\”“ + data +” \\“)”)使它正常工作。 Below are the changes I made: 以下是我所做的更改:

Modify MainActivity.java onData method to: 将MainActivity.java onData方法修改为:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onData(DataEvent event) {
    DeviceClient device = event.getDevice();
    String data = event.getData().getString();           
    loadUrl("javascript:onData(\""+data+"\")");
}

Add this function to my index.js file: 将此函数添加到我的index.js文件中:

function onData(data) {
    inAppBrowserRef && inAppBrowserRef.executeScript({ code: "onData(\""+data+"\")" 
}); // Clear out the command in localStorage for subsequent opens.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM