-1

My requirement is to open an android native simple activity from Cordova Plugin.

I tried the solutions mentioned in this thread. But I am getting error (Sample App, Unfortunately stopped)

I am going crazy over past few days.

Here is my code (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;
        }
    }
}

MainActivity.java

package com.example.sample;

import android.os.Bundle;
import android.app.Activity;
import android.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;
    }
}

Plugin.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="android">

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

</plugin>

Note: When I look at the log in my device in debug mode, it seems like it is not finding MainActivity.java Can anyone help me with this, any sample Cordova Plugin to open an native android activity would help me.

Community
  • 1
  • 1
Prabin Yovan
  • 164
  • 1
  • 3
  • 14

3 Answers3

1

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.java

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_plugin.js

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

config.xml :

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

Here com.example.opennative is the package name and OpenNative is the FileName

To call use window.plugins.openNative.open in your js. No need to import js file. cordova_plugin.js/config.xml will automatically take care of mapping.

Mohammed Imran N
  • 1,279
  • 1
  • 11
  • 26
1

Finally, I got it worked with the help of this plugin

It uses a FakeR.java class to set the layout and all.

Prabin Yovan
  • 164
  • 1
  • 3
  • 14
0
    public static int getId(Context context, String group, String key) {
        return context.getResources().getIdentifier(key, group, context.getPackageName());
    }
Fedir Tsapana
  • 1,283
  • 16
  • 19