Android发送短信有两种主要方式:调起系统发短信功能或调用系统短信接口直接发送。
在Android设备上发送短信可以通过调用系统API或者使用Intent来调起系统的短信应用,以下是关于如何在Android中发送短信的详细步骤:
一、准备工作
1、权限声明:在AndroidManifest.xml
文件中声明发送短信的权限。
<uses-permission android:name="android.permission.SEND_SMS" />
2、用户界面布局:创建一个简单的用户界面,包括输入电话号码和短信内容的文本框以及发送按钮。
<!-activity_main.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/ed_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Phone Number" /> <EditText android:id="@+id/ed_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Message Content" /> <Button android:id="@+id/bt_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send SMS" /> </LinearLayout>
二、代码实现
1. 获取用户输入并发送短信
// MainActivity.java package com.example.sendsms; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private EditText edPhone, edContent; private static final int SEND_SMS = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edPhone = findViewById(R.id.ed_phone); edContent = findViewById(R.id.ed_content); Button btSend = findViewById(R.id.bt_send); btSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { requestPermission(); } }); } private void requestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS); } else { sendSMSS(); } } else { sendSMSS(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == SEND_SMS && grantResults[0] == PackageManager.PERMISSION_GRANTED) { sendSMSS(); } else { Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show(); } } private void sendSMSS() { String phone = edPhone.getText().toString().trim(); String content = edContent.getText().toString().trim(); if (!phone.isEmpty() && !content.isEmpty()) { SmsManager smsManager = SmsManager.getDefault(); List<String> dividedMessages = smsManager.divideMessage(content); for (String text : dividedMessages) { smsManager.sendTextMessage(phone, null, text, null, null); } Toast.makeText(this, "短信发送成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "请填写完整的电话和内容", Toast.LENGTH_SHORT).show(); } } }
2. 参数说明
destinationAddress
:目标电话号码。
scAddress
:短信中心号码,测试可以不填。
text
。
sentIntent
:发送状态的意图,包装了短信发送状态的信息。
deliveryIntent
:接收状态的意图,包装了短信是否被对方收到的状态信息。
三、处理返回的发送状态
为了监控短信的发送状态,可以使用PendingIntent
来处理返回的发送状态和接收状态。
// 处理返回的发送状态 String SENT_SMS_ACTION = "SENT_SMS_ACTION"; Intent sentIntent = new Intent(SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0); context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: case SmsManager.RESULT_ERROR_RADIO_OFF: case SmsManager.RESULT_ERROR_NULL_PDU: break; } } }, new IntentFilter(SENT_SMS_ACTION));
四、处理返回的接收状态
// 处理返回的接收状态 String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0); context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "收信人已经成功接收", Toast.LENGTH_SHORT).show(); } }, new IntentFilter(DELIVERED_SMS_ACTION));
五、完整示例代码汇总
以下是一个完整的示例代码,展示了如何在Android中实现发送短信的功能,包括用户界面、权限请求、短信发送以及发送状态的处理。
// MainActivity.java package com.example.sendsms; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import java.util.List; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText edPhone, edContent; private static final int SEND_SMS = 100; private static final String SENT_SMS_ACTION = "SENT_SMS_ACTION"; private static final String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edPhone = findViewById(R.id.ed_phone); edContent = findViewById(R.id.ed_content); Button btSend = findViewById(R.id.bt_send); btSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { requestPermission(); } }); } private void requestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS); } else { sendSMSS(); } } else { sendSMSS(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == SEND_SMS && grantResults[0] == PackageManager.PERMISSION_GRANTED) { sendSMSS(); } else { Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show(); } } private void sendSMSS() { String phone = edPhone.getText().toString().trim(); String content = edContent.getText().toString().trim(); if (!phone.isEmpty() && !content.isEmpty()) { SmsManager smsManager = SmsManager.getDefault(); List<String> dividedMessages = smsManager.divideMessage(content); for (String text : dividedMessages) { smsManager.sendTextMessage(phone, null, text, null, null); } Toast.makeText(this, "短信发送成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "请填写完整的电话和内容", Toast.LENGTH_SHORT).show(); } } }