Skip to content Skip to sidebar Skip to footer

Android - Is It Possible To Fire A Native Intent From An Application Wrapped In Phonegap

I am developing an application on Sencha Touch 2.0.1 & PhoneGap. I need to catch and transfer an event firing inside Sencha Touch to the native Android environment. i.e: Some s

Solution 1:

I think you'll need to make your own phonegap plugin that launches the native activity from inside it's execute method.

There's a ContactView plugin you should be able to use as a guide for writing your own.

https://github.com/phonegap/phonegap-plugins/blob/master/Android/ContactView/ContactView.java

Specifically these two methods

@OverridepublicPluginResultexecute(String action, JSONArray args, String callbackId) {
    startContactActivity();
    PluginResult mPlugin = newPluginResult(PluginResult.Status.NO_RESULT);
    mPlugin.setKeepCallback(true);
    this.callback = callbackId;
    return mPlugin;
}

publicvoidstartContactActivity() {
    Intent intent = newIntent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
}

Solution 2:

Take a look at this, the explicit and implicit intent sections (1.2, 1.3): http://www.vogella.de/articles/AndroidIntent/article.html

Then take a look at the source code for WebIntent.java, in particular the startActivity function: https://github.com/phonegap/phonegap-plugins/blob/master/Android/WebIntent/WebIntent.java

voidstartActivity(String action, Uri uri, Stringtype, Map<String, String> extras) {
  Intent i = (uri != null ? newIntent(action, uri) : newIntent(action));

And then the intent constructors here (search for Constructors): http://developer.android.com/reference/android/content/Intent.html

WebIntent does not support the Intent constructor that that takes an Android class.

But you can extend the function to have it work with an explicit intent (code below is quick and dirty and untested):

voidstartActivity(String action, Uri uri, Stringtype, String className, Map<String, String> extras) {
  Intent i;
  if (uri != null)
    i = newIntent(action, uri)
  elseif (className != null)
    i = newIntent(this.ctx, Class.forName(className));
  elsenewIntent(action));

Above, in the execute function, you must also parse out the new parameter in the "Parse the arguments" section

// Parse the argumentsJSONObject obj = args.getJSONObject(0);
Stringtype = obj.has("type") ? obj.getString("type") : null;
Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
String className = obj.has("className") ? obj.getString("className") : null;
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;

and then pass the new className string in the call to startActivity a few lines below:

startActivity(obj.getString("action"), uri, type, className, extrasMap);

Then you should be able to call an android activity by classname using something like:

Android.callByClassName = function(className) { 
  var extras = {};
  extras[WebIntent.EXTRA_CUSTOM] = "my_custom";
  extras[WebIntent.EXTRA_CUSTOM2] = "my_custom2";
  window.plugins.webintent.startActivity({
    className: className, 
    extras: extras 
  }, 
  function() {}, 
  function() {
    alert('Failed to send call class by classname');
  }
); 

};

Where the classname is something like: com.company.ActivityName

DISCLAIMER: Rough code, not tested.

Post a Comment for "Android - Is It Possible To Fire A Native Intent From An Application Wrapped In Phonegap"