【问题标题】:How cann I access/send data to MainActivity from WebView Cordova AngularJS controller?如何从 WebView Cordova AngularJS 控制器访问/发送数据到 MainActivity?
【发布时间】:2018-09-20 12:41:33
【问题描述】:

如何将数据从 angularjs 控制器发送到 Android Studio 中的 MainActivity。

这是 MainActivity:

package at.restaurantKellner;

import android.os.Bundle;
import org.apache.cordova.*;

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

这是我的 Angular controller.js:

'use strict';
window.App.Module = angular.module('AppModule.tisch-details', ['ngRoute']);

window.App.Ctrls.controller('tisch-detailsCtrl', ['$scope', '$http', '$location', '$timeout', '$route', '$rootScope', 'ngDialog', 'czs', '$sce', '$q', 'user', function ($scope, $http, $location, $timeout, $route, $rootScope, ngDialog, czs, $sce, $q, user) {

//I want to send this information to MainActivity
$scope.dataForMainActivity = "Data to send";




    }]);

有人可以给我写一个示例代码吗?我将不胜感激

【问题讨论】:

    标签: android angularjs cordova


    【解决方案1】:
    1. 创建 JavascriptWebInterface 类。它可以是一个内部类。

      public class JsInterface {
        Context mContext;
      
        JsInterface(Context c) {
          mContext = c;
        }
      
        @JavascriptInterface   // << This annotation makes it works!
        public void myFunc(String toast) {
          Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
      }
      
    2. 将该类注册到 webView。 Cordova 将其隐藏在 appView 中。

      package at.restaurantKellner;
      
      
      import android.os.Bundle;
      import org.apache.cordova.*;
      
      public class MainActivity extends CordovaActivity
      {
          @Override
          public void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              super.init(); //ADD super.init
      
              // enable Cordova apps to be started in the background
              Bundle extras = getIntent().getExtras();
              if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
                  moveTaskToBack(true);
              }
      
              // INJECT OUR JS INTERFACE HERE!
              WebView webView = (WebView) this.appView.getEngine().getView();
              webView.getSettings().setJavaScriptEnabled(true);
              webView.addJavascriptInterface(new JsInterface(webView.getContext()), "android");
      
              // Set by <content src="index.html" /> in config.xml
              loadUrl(launchUrl);
          }
      }
      
    3. 像普通的 javascript 调用一样进行调用。最好检查一下android是否存在。请注意,android 是窗口中的 javascript 对象。如果您在 Angular 的内部范围内,您可能需要通过window.android 访问它。

      if (typeof android !== 'undefined' && android !== null) {
          android.myFunc('hello');
      } else {
          console.log('Not in android webbox');
      } 
      

    【讨论】:

    • 感谢您的快速回复,不幸的是,当我添加此内容时,应用程序崩溃了。这是我的android项目:ANdroid Project我哪里出错了?
    • super.onCreate(savedInstanceState);之后添加super.init();
    • 有效!谢谢你。我不擅长安卓,你救了我的命。事实上,我需要将 JSON 数据从 POS 打印机发送到 SDK,然后通过 Wifi 将其发送到打印机。如果我需要帮助,我可以在这里联系您吗?非常感谢
    • @ExyuPortal 很高兴我能提供帮助。您可以随时发布问题。我相信这里有很多人可以回答你的问题。
    • 嗨,digitake,我再次需要帮助。我不知道如何将打印机 SDK 与应用程序连接起来。这是用于打印机的Android SDK:SDK Download我需要先连接打印机metapacePrinter = new MetapacePrinter(this, mHandler, null); String hostx; hostx = "192.168.1.25"; metapacePrinter.connect(hostx, 9100, 5000);然后将JSON数据发送到打印,它在PrintTextActivity类中...... private void printText()......你能帮我吗?
    猜你喜欢
    • 2014-02-11
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2018-06-15
    • 2016-10-19
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多