32

I have written a CordavaPlugin derived class.

public class ShowMap extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args,
        CallbackContext callbackContext) throws JSONException {

    if (action.compareTo("showMap") == 0)
    {
        String message = args.getString(0); 
        this.echo(message, callbackContext);

        Intent i = new Intent();


        return true;
    }

    return false;
}

private void echo(String message, CallbackContext callbackContext) {
    if (message != null && message.length() > 0) { 
        callbackContext.success(message);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}

}

I want from this class to open a new activity. How do I get access to the original context of the phonegap based class?

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
krasnoff
  • 847
  • 3
  • 17
  • 34

6 Answers6

42

try as:

    Context context=this.cordova.getActivity().getApplicationContext();
    //or Context context=cordova.getActivity().getApplicationContext();
    Intent intent=new Intent(context,Next_Activity.class);

    context.startActivity(intent);
    //or cordova.getActivity().startActivity(intent);

and make sure you have registered Next Activity in AndroidManifest.xml

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Heads up in case anyone makes the same mistake I did — my CallbackContext argument was also called context and took me a while to spot the problem with my plugin! – Matt Lacey Nov 04 '13 at 01:54
  • and how can i call a callback from that activity class? – Lithu T.V Sep 24 '14 at 00:54
  • This doesn't answer the question and is no longer relevant as of whenever time the tag was required to be nested within , as there is no way that I can see to add an activity to AndroidManifest within the tag. Saying "make sure you have registered Next Activity in AndroidManifest.xml" is glazing over a huge undocumented requirement. – Tom Pace Jul 05 '17 at 17:03
  • This Q&A was the top google search result for "cordova plugin activity". I've read the Cordova plugin guide and focussed on android. Those docs go into adding a custom `Activity` and receiving a result, notably if underlying Cordova activity is destroyed. All told, there is STILL a crucial step missing! Missing both in their docs and this answer. The goes inside the tag, and they don't clearly describe the injection code for plugin.xml ... – Tom Pace Jul 05 '17 at 17:40
16
  1. Register your activity in the AndroidManifest file
  2. In your plugin you should have the code like this, notice no "callback.success()" is called
  3. Run the action in ui thread and not background thread.
  4. enjoy

    if (action.equals("myaction")) {
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Context context = cordova.getActivity()
                        .getApplicationContext();
                Intent intent = new Intent(context, MyNewActivityGap.class);
                cordova.getActivity().startActivity(intent);
            }
        });
    
        return true;
    }
    
P.Ranjan
  • 171
  • 1
  • 6
5
Context context =  cordova.getActivity().getApplicationContext();
Intent intent = new Intent(context,Next_Activity.class);

cordova.startActivityForResult(this, intent,0);
zainoz.zaini
  • 918
  • 6
  • 20
2

Post now in 2017, because it's the top-ranked google search result for "cordova plugin activity" and top-voted answer, along with Cordova plugin guide are both missing the following critical information, that took me many hours to figure out... the parent attrib of config-file and the specific code:

Added to plugin.xml, customized according to your needs:

<!-- separate config-file here targeting AndroidManifest with parent NOT equal to /* -->
<config-file target="AndroidManifest.xml"
    parent="/manifest/application">
    <activity
        android:name=com.custompackage.MyCustomActivity">
    </activity>         
</config-file>

Updating launching code with above package & activity:

Context context=this.cordova.getActivity().getApplicationContext();
//or Context context=cordova.getActivity().getApplicationContext();
Intent intent=new Intent(context, com.custompackage.MyCustomActivity.class);

context.startActivity(intent);
//or cordova.getActivity().startActivity(intent);
Tom Pace
  • 2,347
  • 1
  • 24
  • 32
1

I used implicit intent go get this functionality working

  Intent i = new Intent("ACTION_PLAY_VIDEO");
 this.cordova.startActivityForResult((CordovaPlugin) this,i, 0);

don't forget to put intent filter in your target activity in manifest file

<activity android:name="VideoPlayerActivity" >
       <intent-filter>
            <action android:name="ACTION_PLAY_VIDEO" />


            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

-2

See this example.

Firstly you need to declare your custom plugin in config.xml. You can found this file in res > xml folder.

<feature name="CustomPlugin">
      <param name="android-package" value="com.Phonegap.CustomPlugin" />
</feature>

Then you need to implement plugin by using Java- code

public class CustomPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {

        if (action.equals("sayHello")){
            try {
                String responseText = "Hello world, " + args.getString(0);
                callbackContext.success(responseText);
            } catch (JSONException e){
                callbackContext.error("Failed to parse parameters");
            }
            return true;
        }

        return false;
    }
}

Finally we calling a plugin from javascript

function initial(){
    var name = $("#NameInput").val();
    cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
}

function sayHelloSuccess(data){
    alert("OK: " + data);
}

function sayHelloFailure(data){
    alert("FAIL: " + data);
}
Paulo Miguel Almeida
  • 2,114
  • 31
  • 36
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • This post doesn't address the initial question: '[How do I] open an activity from a cordova plugin?' Rather, it just tells you how to write a cordova plugin. – babycakes Oct 07 '16 at 22:03