gpt4 book ai didi

android - 发送短信不会启动 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 08:30:15 24 4
gpt4 key购买 nike

我想从我的应用程序发送短信,为此我编写了下面的代码,这非常简单。但我面临的问题是,发送消息时未启动任何 Activity

Method to send a message:

private void sendSMS(Context context, String phone, String msg){
Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.setData(Uri.parse("smsto:"));
smsIntent.putExtra("address", phone);
smsIntent.putExtra("sms_body", msg);
smsIntent.setType("vnd.android-dir/mms-sms");

try {
startActivity(smsIntent);
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
}
}

Permission added in manifest

<uses-permission android:name="android.permission.SEND_SMS" />

总是显示用catch()写的toast

最佳答案

试试这个:

也没有必要:

<uses-permission android:name="android.permission.SEND_SMS" />

只需使用这段代码:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", "Check out My app for your smartphone. Download it today from https://google.com");
try {
startActivity(sendIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}

关于android - 发送短信不会启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com