环境:Android-studio
安卓版本:安卓6
1.先创建一个项目吧
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the phone number of the recipient"/>
<EditText
android:id="@+id/phoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message"/>
<EditText
android:id="@+id/textMessage"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:maxLength="160"
android:gravity="top"/>
<TextView
android:id="@+id/mCounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="160/0"/>
<Button
android:id="@+id/sendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send SMS"/>
</LinearLayout>
3.写个接收类吧
package com.example.sendsms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle!=null){
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0;i<msgs.length;i++){
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from "+msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "n";
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
4.清单文件给权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsms" >
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Map" >
<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5.主活动
package com.example.sendsms;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button sendSMS;
EditText phoneNo;
EditText textMessage;
TextView mCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendSMS = findViewById(R.id.sendSMS);
phoneNo = findViewById(R.id.phoneNo);
textMessage = findViewById(R.id.textMessage);
mCounter = findViewById(R.id.mCounter);
textMessage.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
String smsLength = String.valueOf(160 - s.length());
mCounter.setText(smsLength + "/" + s.length());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
//发送按钮
sendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobNo = phoneNo.getText().toString();
String message = textMessage.getText().toString();
if(mobNo.length() > 0 && message.length() > 0)
sendSms(mobNo, message);
else
Toast.makeText(getBaseContext(), "message shouldn't null.", Toast.LENGTH_SHORT).show();
}
});
}
public void sendSms(String mobNo,String message){
String smsSent = "SMS_SENT";
String smsDelivered = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this,0,new Intent(smsSent),0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,0,new Intent(smsDelivered),0);
//发送结果广播
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Result OK", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "NO_SERVICE", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "NULL_PDI", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "RADIO_OFF", Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(smsSent));
//短信接收广播
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(smsDelivered));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(mobNo,null,message,sentPI,deliveredPI);
Toast.makeText(getApplicationContext(), "send successful", Toast.LENGTH_SHORT).show();
}
}
6.运行吧
7.记得在虚拟机也给应用权限