我正在使用cordova和html(js / css等...)构建一个Android应用程序 我正在尝试在用户点击链接时打开短信应用程序。
这是html代码:
<a href="sms:052xxxx808">Send Sms</a>
虽然tel:
和mailto:
计划有效,但sms:
或smsto:
无效。
信息:
使用sms:
方案我收到此错误:No Activity found to handle Intent
E/Cordova(28360): Error sending sms sms:052xxxx808:android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }
使用sms:
方案我收到此错误:ERR_UNKNOWN_URL_SCHEME
D/Cordova(27207): CordovaWebViewClient.onReceivedError: Error code=-10 Description=net::ERR_UNKNOWN_URL_SCHEME URL=smsto:052xxxx808
我正在使用Nexus 5和Kitkat 4.4.2来测试应用程序..
P.S:在Galaxy 4上,sms:
方案正在运行......(不是KitKat)
更新
使用sms:
方案点击浏览器中的href链接确实有效,所以我可能错过了许可或类似内容吗?
答案 0 :(得分:0)
我能够通过创建一个结合了前两个答案here和here的短信插件来实现这一目标。唯一的问题是,您必须检查构建版本是否与KitKat不同的意图。
这是我的插件代码:
public class SmsPlugin extends CordovaPlugin {
public final String ACTION_SEND_SMS = "SendSMS";
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION_SEND_SMS)) {
try {
String phoneNumber = args.getString(0);
String message = args.getString(1);
String method = args.getString(2);
if (method.equalsIgnoreCase("INTENT")) {
invokeSMSIntent(phoneNumber, message);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
} else {
sendSMS(phoneNumber, message);
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
return true;
} catch (JSONException ex) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
return false;
}
private void invokeSMSIntent(String phoneNumber, String message) {
Intent intent;
Activity activity = this.cordova.getActivity();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity);
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(phoneNumber)));
intent.putExtra("sms_body", message);
// Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
if (defaultSmsPackageName != null) {
intent.setPackage(defaultSmsPackageName);
}
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", phoneNumber);
intent.putExtra("sms_body", message);
}
activity.startActivity(intent);
}
private void sendSMS(String phoneNumber, String message) {
SmsManager manager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0);
manager.sendTextMessage(phoneNumber, null, message, sentIntent, null);
}
}
以下是我从JavaScript调用它的方法:
var sms = function() {
message: function (phnum, callback) {
if (Ext.os.is.iOS) {
cordova.exec(callback, function (err) {
callback('The following error occurred: ' + err);
}, "Sms", "send", [ {"recipients": [phnum]} ]);
} else if (Ext.os.is.Android) {
cordova.exec(callback, function (err) {
callback('The following error occurred: ' + err);
}, "SmsPlugin", "SendSMS", [phnum, "", "INTENT"] );
} else {
document.location.href = "sms:" + phnum
}
}
};
module.exports = sms;
请务必将此添加到config.xml:
<feature name="SmsPlugin">
<param name="android-package" value="my.plugin.SmsPlugin" />
</feature>