Android 发送短信
Posted DaLiangChen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 发送短信相关的知识,希望对你有一定的参考价值。
一、发送短信的方法
- 使用 Intent 隐式调用系统短信应用进行发送
Uri uri = Uri.parse("smsto:10086");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "Hello World!");
startActivity(it);
- 使用 SmsManager 发送
//获取SmsManager
SmsManager sms=SmsManager.getDefault();
//如果内容大于70字,则拆分为多条
List<String> texts=sms.divideMessage(“需要发送的短信内容!”);
//逐条发送短信
for(String text:texts) {
sms.sendTextMessage(10086, null, text, null, null);
}
二、发送短信Demo
1.界面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/ed_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:maxLength="11"
android:hint="请输入手机号码"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/ed_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="请输入需要发送的短信内容"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ed_phone" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="发送短信"
android:onClick="onClickSendSMSButton"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ed_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.MainActivity 代码如下
package com.daliang.demo_sms;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int SEND_SMS = 100;
private EditText edContent;
private EditText edPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edContent=findViewById(R.id.ed_content);
edPhone=findViewById(R.id.ed_phone);
}
//点击了发送短信按钮
public void onClickSendSMSButton(View view) {
requestPermission();
}
//请求权限
private void requestPermission() {
//判断Android版本是否大于23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS);
return;
} else {
sendSMS();
}
} else {
sendSMS();
}
}
//权限请求回调结果
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case SEND_SMS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
Toast.makeText(this, "请求权限被拒绝!", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
//发送短信
private void sendSMS()
{
String content=edContent.getText().toString().trim();
String phone=edPhone.getText().toString().trim();
if (content.isEmpty()||phone.isEmpty()) {
Toast.makeText(this, "电话号码或短信内容不能为空!", Toast.LENGTH_LONG).show();
return;
}
//获取SmsManager
SmsManager sms=SmsManager.getDefault();
//如果内容大于70字,则拆分为多条
List<String> texts=sms.divideMessage(content);
//逐条发送短信
for(String text:texts) {
sms.sendTextMessage(phone, null, text, null, null);
}
Toast.makeText(this,"发送短信成功!",Toast.LENGTH_LONG).show();
}
}
3.需要在 AndroidManifesh.xml 中添加发送短信权限
<uses-permission android:name="android.permission.SEND_SMS"/>
以上是关于Android 发送短信的主要内容,如果未能解决你的问题,请参考以下文章