java使用intent例子,Java Intent.resolveActivityInfo方法代码示例

本文介绍了一种在Cordova应用中启动Android Intent的方法。该方法通过定义一系列参数来配置Intent,包括Action、URI、类型及额外数据等,并根据期望的结果与否决定回调方式。

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值