【问题标题】:Access and use in Codename One native Android classes在 Codename One 原生 Android 类中访问和使用
【发布时间】:2015-12-20 19:21:00
【问题描述】:

我知道 Codename One 和 Android 是完全不同的框架。但问题是,如何从原生 Android 访问 Codename One 类?

例如:我想创建一个名为onCreat() 的方法,如下所示:

public void onCreate(Bundle savedInstanceState){}

在Android中你必须导入包import android.os.Bundle;

如何将这个包(用于onCreat())和其他来自 Android 的包集成到代号 One 中?

我应该在 Code Name One 中设置什么才能访问这些类。这可能吗?

编辑:

另一个例子:

  import android.net.Uri;
  public void startWeb(View v) {
       Uri uri = Uri.parse("http://www.google.com");
       Intent intent = new Intent(Intent.ACTION_VIEW, uri);
       startActivity(intent);    
    }

如何在 Codename One 中转换此 Android 代码,使其看起来像这样,如果我单击按钮,则可以:

 protected void onMain_Button6Action(Component c, ActionEvent event) {
       Uri uri = Uri.parse("http://www.google.com");
       Intent intent = new Intent(Intent.ACTION_VIEW, uri);
       startActivity(intent);    
    }

重点是我想了解如何让 Codename One 的 Android 代码发挥作用。

谢谢。

【问题讨论】:

    标签: android codenameone


    【解决方案1】:

    您需要使用native interfaces 来访问android 原生功能。但是,onCreate 调用是一种特殊情况,因为它是来自 Android 应用程序生命周期的回调,将被调用以启动 Codename One 启动/初始化方法,并且不会调用您的代码。

    要监听这些生命周期调用,您可以使用 AndroidNativeUtil 类,该类有一个生命周期监听器,允许您从本机接口访问这些功能。

    【讨论】:

      【解决方案2】:

      包 com.mycompany.ecopay;

      import android.content.Intent;
      import android.net.Uri;
      import com.codename1.impl.android.AndroidNativeUtil;
      import com.codename1.impl.android.CodenameOneActivity;
      
      
      public class NativeAccessImpl {
      public void pay() {           
      com.codename1.impl.android.AndroidNativeUtil.getActivity().runOnUiThread(new  
      Runnable() {
      public void run() {
      // your code goes here
      CodenameOneActivity aActivity = (CodenameOneActivity)        
      AndroidNativeUtil.getActivity();
      android.content.Intent intent = new                    
      android.content.Intent(Intent.ACTION_CALL, Uri.parse("tel:0779083353"));
      aActivity.startActivity(intent);
              }
          });
      }
      public boolean isSupported() {
          return true;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-12
        • 2016-04-24
        • 1970-01-01
        相关资源
        最近更新 更多