0

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 sencha touch-controlled-buttons need to fire an intent on click to start another activity (non-PhoneGap activities).

So far I have found various examples like webintents and this. But as far as I see, these are inapplicable in my case.

I seek to either drop PhoneGap and work with another wrapper, or somehow circumvent this issue. Thanks in advance!

Community
  • 1
  • 1
Mitraidos
  • 13
  • 1
  • 4

2 Answers2

2

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

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

public void startContactActivity() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    this.ctx.startActivityForResult((Plugin) this, intent, PICK_CONTACT);
}
Jason Freitas
  • 1,587
  • 10
  • 18
  • +1 That was a useful link https://github.com/phonegap/phonegap-plugins/blob/master/Android/ContactView/ContactView.java – Spiff Jul 10 '12 at 12:50
0

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

void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
  Intent i = (uri != null ? new Intent(action, uri) : new Intent(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):

void startActivity(String action, Uri uri, String type, String className, Map<String, String> extras) {
  Intent i;
  if (uri != null)
    i = new Intent(action, uri)
  else if (className != null)
    i = new Intent(this.ctx, Class.forName(className));
  else
    new Intent(action));

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

// Parse the arguments
JSONObject obj = args.getJSONObject(0);
String type = 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.

Theo
  • 5,963
  • 3
  • 38
  • 56