import android.content.Intent; //导入方法依赖的package包/类
private void startActivity(String action, Uri uri, String type, Map extras, boolean bExpectResult, int requestCode, CallbackContext callbackContext) {
// Credit: https://github.com/chrisekelley/cordova-webintent
Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));
if (type != null && uri != null) {
i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
} else {
if (type != null) {
i.setType(type);
}
if (uri != null)
{
i.setData(uri);
}
}
for (String key : extras.keySet()) {
String value = extras.get(key);
// If type is text html, the extra text must sent as HTML
if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
i.putExtra(key, Html.fromHtml(value));
} else if (key.equals(Intent.EXTRA_STREAM)) {
// allowes sharing of images as attachments.
// value in this case should be a URI of a file
final CordovaResourceApi resourceApi = webView.getResourceApi();
i.putExtra(key, resourceApi.remapUri(Uri.parse(value)));
} else if (key.equals(Intent.EXTRA_EMAIL)) {
// allows to add the email address of the receiver
i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
} else {
i.putExtra(key, value);
}
}
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (i.resolveActivityInfo(this.cordova.getActivity().getPackageManager(), 0) != null)
{
if (bExpectResult)
{
cordova.setActivityResultCallback(this);
((CordovaActivity) this.cordova.getActivity()).startActivityForResult(i, requestCode);
}
else
{
((CordovaActivity)this.cordova.getActivity()).startActivity(i);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
}
else
{
// Return an error as there is no app to handle this intent
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
}
}
本文介绍了一种在Cordova应用中启动Android Intent的方法。该方法通过定义一系列参数来配置Intent,包括Action、URI、类型及额外数据等,并根据期望的结果与否决定回调方式。
374

被折叠的 条评论
为什么被折叠?



