下面的代码在android版本的lollipop上工作正常,但在较新版本的android sms主体上却无法自动填充。即使在给予必需的权限后,它也会打开没有预填充文本的相应数字。请提出解决方案
smsPermission = async() => {
try {
const isGranted = await PermissionsAndroid.check( PermissionsAndroid.PERMISSIONS.SEND_SMS )
if(!isGranted) {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.SEND_SMS,
{
title: 'Send SMS Permission',
message: 'jose2007kj need permission to send invite through sms',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
var sparator = Platform.OS == 'ios' ? '&' : '?';
var message = `Check ${this.props.user.username}'s profile on jose2007kj now ${API.BASE_URL}/pages/@${this.props.user.username}`;
Linking.openURL('sms:'+`${this.state.invite_number}`+sparator+'body=' + message)
}
}else{
var sparator = Platform.OS == 'ios' ? '&' : '?';
var message = `Check ${this.props.user.username}'s profile on jose2007kj now ${API.BASE_URL}/pages/@${this.props.user.username}`;
Linking.openURL('sms:' + `${this.state.invite_number}` + sparator + 'body=' + message)
}
} catch (err) {
console.warn(err);
}
}
答案 0 :(得分:1)
可能与此restrictions有关。要在新的android版本中发送短信,请使用此Intent(我不知道react-native的方式,但可能必须一样)
val intent = Intent().apply {
action = Intent.ACTION_SENDTO
data = Uri.parse("smsto:0123456789")
putExtra("sms_body", "text message")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
并删除权限,因为如果未向Google发送使用此权限的原因,则具有短信组权限的应用将从Google Play中删除。
答案 1 :(得分:0)
我可以使用下面的代码来使其工作
@ReactMethod
public void sendSms(String mobileNo,String message){
try{
Intent sendIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(reactContext);
sendIntent = new Intent(Intent.ACTION_SEND);
if (defaultSmsPackageName != null){
sendIntent.setPackage(defaultSmsPackageName);
}
sendIntent.setType("text/plain");
}else {
sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
}
sendIntent.putExtra("sms_body", message);
sendIntent.putExtra("exit_on_sent", true);
sendIntent.putExtra("address", mobileNo);
reactContext.startActivity(sendIntent);
}catch(Exception e){
Log.d("sms ", "sms error: "+e.toString());
}
}
参考
Davids关于permissions,usage of Action_sendto的详细说明
我从react-native-sms那里获得了这段代码