简体   繁体   English

如何从我的Android应用发送短信?

[英]How can i send SMS from my android app?

I am trying to send an SMS from my android app. 我正在尝试从我的Android应用发送短信。 Every time I click on my button, it goes to catch part rather then try . 每次单击按钮时,它都会catch而不是try

Here is my method: 这是我的方法:

Button sms = (Button)findViewById(R.id.button2);
    sms.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {

                    Log.i("Send SMS", "");
                    Intent sms = new Intent(Intent.ACTION_VIEW);
                    sms.setData(Uri.parse("smsto:"));
                    sms.setType("vnd.android-dir/mms-sms");
                    sms.putExtra("address", new String(con));
                    sms.putExtra("sms_body", "Test SMS to Angilla");

                    try {
                        startActivity(sms);
                        finish();
                        Log.i("Finished sending SMS...", "");
                    }
                    catch (android.content.ActivityNotFoundException ex) {
                        Toast.makeText(DetailActivity.this,
                                "SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
                    }

                }
            });

You can send sms two ways: 1. SMS Manager 2. Built in app 您可以通过两种方式发送短信:1. SMS Manager 2.内置应用

Both ways need permissions in manifest 两种方式都需要清单中的权限

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

Example with sms manager: 短信管理器示例:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);

Example with built-in app: 内置应用程序示例:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content"); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

This is working for me : 这为我工作:

Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("smsto:"));
sendIntent.putExtra("sms_body", "Your message");

try {
    startActivity(sendIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ex.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的应用程序向其他Android设备发送短信 - How to send SMS message to another android device from my app 如何将短信从我的短信应用发送到应用 - how to send sms from my sms app to app 我如何从我的应用程序发送短信 - How can i send sms from my application 如何将短信从Android发送到Azure IoT中心 - How can I send SMS from Android to Azure IoT Hub 如何从Android应用程序向移动设备发送短信? - How to send sms from android app to mobile? 如何为我的App android设置默认短信权限? - How can i set default SMS permission to my App android? 我可以从Android手机发送闪光短信吗? 和q.2。 如何从手机发送短信,而不显示发件人的号码? - can i send a flash sms from my android handset ? and q.2. how to send sms from mobile , without displaying sender's number? 如何将文件从服务器发送到Android应用程序? - How I can send a file from my server to an Android app? 我如何从我的Android应用程序发送Skype消息? - how can i send a skype message from My android app? 如何在我的 Android 应用程序中使用用户自己的短信服务发送 OTP 并进行手机号码验证? - How can I use a user's own SMS service to send OTP and make mobile number verification in my Android app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM