Android - Is it possible to fire a native intent from an application wrapped in PhoneGap

一世执手 提交于 2019-12-25 01:05:17

问题


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!


回答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

    @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);
}



回答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

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.



来源:https://stackoverflow.com/questions/10043272/android-is-it-possible-to-fire-a-native-intent-from-an-application-wrapped-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!