【问题标题】:Access android native code from Ionic从 Ionic 访问 android 本机代码
【发布时间】:2018-07-06 14:40:42
【问题描述】:

我已经创建了我的 Android 应用程序 Ionic,但后来意识到我想从我的 android 设备访问一些硬件,这可以使用 android 本机代码,所以有什么方法可以从 Ionic 应用程序访问 Android 代码(Apache科尔多瓦)?

【问题讨论】:

    标签: android cordova ionic-framework cross-platform cordova-plugins


    【解决方案1】:

    您可以使用 JS 接口桥从 JS 控制器调用 Native 方法:

    1. 在 MainActivity 中定义一个 JavaScriptHandler 类并定义 此类中的 JavascriptInterface 方法。这个界面现在将起作用 作为 webview 和原生容器之间的桥梁。
    2. 将此 Javascript 处理程序添加到您的应用 web 视图中。
    3. 定义需要从控制器调用的方法。

    下面是扩展 CordovaActivity 的 MainActivity 代码:

    public class MainActivity extends CordovaActivity {
      public static Context mContext;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = MainActivity.this;
        mLoader = new ProgressDialog(mContext);
    
        // 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);
    
        final WebView webView = (WebView) appView.getEngine().getView();
        webView.addJavascriptInterface(this.getJavaScriptHandler(), "NativeCall");
      }
    
      public JavaScriptHandler getJavaScriptHandler() {
        return new JavaScriptHandler(this.getApplicationContext());
      }
    
      public class JavaScriptHandler {
        CordovaActivity parentActivity;
        private Context mContext;
    
        public JavaScriptHandler(final Context context) {
          this.mContext = context;
        }
    
        @JavascriptInterface
        public String mNativeFunction() {
          return getDeviceName;
        }   
      }
    
      public static String getDeviceName() {
        String brand = Build.BRAND;
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.startsWith(brand)) {
          return (model);
        }
        return (brand) + " " + model;
      }
    }
    

    从你的 Javascript 控制器调用这个本地方法,如下所示:

    MyDemoApp.controller('SomePageController', function ($scope) {
    
      $scope.deviceName = "";
    
      $scope.getDeviceName = function () {
          $scope.deviceName = NativeCall.mNativeFunction();
      }
    })
    

    您可以使用任何您喜欢的名称来在 javascript 中公开 JavaScriptHandler。就我而言,我使用了“NativeCall”。

    【讨论】:

    • 我也在我的 android 代码中使用其他依赖项,这就像打印条形码和访问打印机,因此我想在 Ionic3 应用程序中使用所有这些代码
    • 您可能需要考虑实现一个自定义的 cordova 插件。
    • 是的,但如果是插件,我如何管理多个文件和依赖的 jar 文件
    • 我不确定你在说什么多个文件。你能更精确一点吗?但是 jars 可以放在你的 lib 文件夹中,在 platforms->android->app 中,并将这一行添加到你的 build.gradle 文件中以编译它: implementation fileTree(dir: 'libs', include: '*.jar') .Similarly ,也可以将依赖项添加到您的 build.gradle 中。
    • 谢谢我从你的评论中得到了这个想法,会试试这个
    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多