代码之家  ›  专栏  ›  技术社区  ›  Exyu Portal digitake

如何从WebView Cordova AngularJS控制器访问/向MainActivity发送数据?

  •  0
  • Exyu Portal digitake  · 技术社区  · 8 年前

    如何将数据从angularjs控制器发送到Android Studio中的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);
        }
    }

    这是我的角度控制器。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 Studio files

    有人能给我写一个示例代码吗?我将非常感激

    1 回复  |  直到 6 年前
        1
  •  2
  •   Exyu Portal digitake    8 年前
    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');
      }