Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open an activity from a CordovaPlugin

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?

like image 616
krasnoff Avatar asked Jan 14 '13 15:01

krasnoff


4 Answers

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

like image 85
ρяσѕρєя K Avatar answered Oct 15 '22 18:10

ρяσѕρєя K


  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;
    }
    
like image 29
P.Ranjan Avatar answered Oct 15 '22 18:10

P.Ranjan


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

cordova.startActivityForResult(this, intent,0);
like image 5
zainoz.zaini Avatar answered Oct 15 '22 17:10

zainoz.zaini


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);
like image 2
Tom Pace Avatar answered Oct 15 '22 17:10

Tom Pace