Android - 是否可以从PhoneGap中包含的应用程序激发本机意图

时间:2012-04-06 12:06:32

标签: javascript android cordova extjs

我正在使用Sencha Touch 2.0.1开发应用程序& PhoneGap的。
我需要捕获并将Sencha Touch中的事件传输到原生Android环境。

即:某些sencha触控按钮需要触发点击意图以启动另一个活动(非PhoneGap活动)。

到目前为止,我找到了各种示例,例如webintentsthis。但据我所知,这些在我的案例中是不适用的。

我试图放弃PhoneGap并使用另一个包装器,或以某种方式绕过这个问题。提前谢谢!

2 个答案:

答案 0 :(得分:2)

我认为你需要制作自己的phonegap插件,从其执行方法中启动本机活动。

有一个ContactView插件,您应该可以将其用作编写自己的插件。

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

特别是这两种方法

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

答案 1 :(得分:0)

看看这个,显式和隐式意图部分(1.2,1.3): http://www.vogella.de/articles/AndroidIntent/article.html

然后看一下WebIntent.java的源代码,特别是startActivity函数: 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));

然后是这里的intent构造函数(搜索构造函数): http://developer.android.com/reference/android/content/Intent.html

WebIntent不支持采用Android类的Intent构造函数。

但是您可以扩展该函数以使其具有明确的意图(下面的代码是快速,脏和未经测试的):

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

上面,在execute函数中,你还必须在“解析参数”部分中解析出新参数

// 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;

然后在调用startActivity的下面几行传递新的className字符串:

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

然后你应该能够通过类名调用android活动:

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

};

其中classname类似于:com.company.ActivityName

免责声明:粗糙的代码,未经过测试。