有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Cordova Android插件从Cordova插件打开Android本机活动

我的要求是从Cordova插件中打开一个安卓原生简单活动

我尝试了this thread中提到的解决方案。但我遇到了错误(示例应用,不幸停止)

过去几天我都快疯了

这是我的代码(Hello.java)

package com.example.sample;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import com.example.sample.MainActivity;

public class Hello extends CordovaPlugin
{
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
    {
       if (action.equals("greet")) 
        {
            Context context=this.cordova.getActivity().getApplicationContext();
            Intent intent=new Intent(context,MainActivity.class);
            context.startActivity(intent);
            return true;
        }
        else
        {
            return false;
        }
    }
}

主要活动。爪哇

package com.example.sample;

import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

插件。xml

<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
        id="com.example.sample"
        version="0.7.0">

  <name>Hello</name>

  <engines>
    <engine name="cordova" version=">=3.4.0"/>
  </engines>

  <asset src="www/hello.js" target="js/hello.js"/>

  <js-module src="www/hello.js" name="hello">
    <clobbers target="hello" />
  </js-module>

  <platform name="安卓">

    <config-file target="res/xml/config.xml" parent="/*">
      <feature name="Hello">
        <param name="安卓-package" value="com.example.sample.Hello"/>
      </feature>
    </config-file>
    <source-file src="src/安卓/src/com/example/sample/Hello.java" target-dir="src/com/example/sample/"/>
    <source-file src="src/安卓/src/com/example/sample/MainActivity.java" target-dir="src/com/example/sample/"/>
    <source-file src="src/安卓/src/com/example/sample/R.java" target-dir="src/com/example/sample/"/>
  </platform>

</plugin>

注意: 当我在调试模式下查看设备中的日志时,它似乎没有找到MainActivity。JAVA 有人能帮我吗?任何打开原生安卓活动的Cordova插件都能帮我


共 (3) 个答案

  1. # 1 楼答案

    最后,在this plugin的帮助下,我终于成功了

    它用的是赝品。java类来设置布局和所有

  2. # 2 楼答案

        public static int getId(Context context, String group, String key) {
            return context.getResources().getIdentifier(key, group, context.getPackageName());
        }
    
  3. # 3 楼答案

    opennative。js

    cordova.define("com.example.opennative.OpenNative", function(require, exports, module) { 
    
    function OpenNative() {
    };
    
    OpenNative.prototype.open = function(callbackContext) {
        callbackContext = callbackContext || {};
        cordova.exec(callbackContext.success || null, callbackContext.error || null, "OpenNative", "open", []);
    
    };
    
    /**
     * Load Plugin
     */
    
    if(!window.plugins) {
        window.plugins = {};
    }
    if (!window.plugins.openNative) {
        window.plugins.openNative = new OpenNative();
    }
    
    });
    

    OpenNative。爪哇

    public class OpenNative extends CordovaPlugin {
    
        /**
         * Executes the request and returns a boolean.
         * 
         * @param action
         *            The action to execute.
         * @param args
         *            JSONArry of arguments for the plugin.
         * @param callbackContext
         *            The callback context used when calling back into JavaScript.
         * @return boolean.
         */
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
                    if (action.equals("open")) {
                            try {
                                openN();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        else {
                        }
                        return true;
                }
                private void openN() throws IOException {
    
                    intent = new Intent(this.cordova.getActivity().getApplicationContext(), Second.class);
    
                    this.cordova.getActivity().startActivityForResult(intent,0);
                    this.cordova.getActivity().startActivity(intent);
                    this.cordova.getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                }
    
            }
    

    cordova_插件。js

       {
            "file": "plugins/com.example.opennative/www/fileOpener.js",
            "id": "com.example.opennative.OpenNative",
            "clobbers": [
                "openNative.open"
            ]
        }
    

    配置。xml:

     <feature name="OpenNative">
            <param name="android-package" value="com.example.opennative.OpenNative" />
        </feature>
    

    这里是com。实例opennative是包名,opennative是文件名

    要调用,请使用窗口。插件。openNative。在js中打开。无需导入js文件。 cordova_插件。js/config。xml将自动处理映射