一、布局
<?xml version="1.0" encoding="utf-8"?>二、清单文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<Button
android:id="@+id/btn_sms_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"/>
<TextView
android:id="@+id/txt_sms_sender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="短信的发送者"
android:textColor="#000"/>
<TextView
android:id="@+id/txt_sms_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="接受的短信内容"
android:textColor="#000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="发送的电话号码"
android:textColor="#000"/>
<EditText
android:id="@+id/edt_sendtel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edt_sendcontent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>三、实现功能的代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.test">
<!--接收短信的权限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<!--发送短信的权限-->
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".NotificationActivity"></activity>
<activity android:name=".SMSActivity"></activity>
<activity android:name=".TableActivity"></activity>
<activity android:name=".AddTableActivity"></activity>
</application>
</manifest>
package com.example.administrator.test;
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.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* 发送接受短信的界面
*/
public class SMSActivity extends AppCompatActivity implements View.OnClickListener {
Button mButton_back;
TextView mTextView_sender;
TextView mTextView_content;
IntentFilter receiveFilter, sendFilter;
MessageReceiver mMessageReceiver;
SendStatusReceiver mStatusReceiver;
EditText mText_tel;
EditText mText_content;
Button mButton_send;
String tel;
String smscontent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms);
initView();
}
private void initView() {
mButton_back = (Button) findViewById(R.id.btn_sms_back);
mTextView_content = (TextView) findViewById(R.id.txt_sms_content);
mTextView_sender = (TextView) findViewById(R.id.txt_sms_sender);
mText_tel = (EditText) findViewById(R.id.edt_sendtel);
mText_content = (EditText) findViewById(R.id.edt_sendcontent);
mButton_send = (Button) findViewById(R.id.btn_send);
mButton_send.setOnClickListener(this);
mButton_back.setOnClickListener(this);
//动态注册广播接收短信的广播
receiveFilter = new IntentFilter();
receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
mMessageReceiver = new MessageReceiver();
registerReceiver(mMessageReceiver, receiveFilter);
//动态注册接 收发送短信状态的的广播
sendFilter = new IntentFilter();
sendFilter.addAction("SENT_SMS_ACTION");
mStatusReceiver = new SendStatusReceiver();
registerReceiver(mStatusReceiver, sendFilter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sms_back:
this.finish();
break;
case R.id.btn_send:
tel = mText_tel.getText().toString();
if (tel.equals("")) {
Toast.makeText(this, "请输入要发送的电话号码!", Toast.LENGTH_SHORT).show();
return;
}
smscontent = mText_content.getText().toString();
if (smscontent .equals("")) {
Toast.makeText(this, "请输入要发送的内容!", Toast.LENGTH_SHORT).show();
return;
}
SmsManager manager = SmsManager.getDefault();
Intent intent = new Intent("SENT_SMS_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(SMSActivity.this, 0, intent, 0);
manager.sendTextMessage(tel, null, smscontent, pendingIntent, null);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mMessageReceiver);
unregisterReceiver(mStatusReceiver);
}
//定义广播,用来监视发送短信的状态
class SendStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("发送状态", "-----" + getResultCode());
if (getResultCode() == RESULT_OK) {
//短信发送成功
Toast.makeText(context, "短信发送成功!", Toast.LENGTH_SHORT).show();
mText_tel.setText("");
mText_content.setText("");
} else {
//短信发送失败
Toast.makeText(context, "短信发送失败!", Toast.LENGTH_SHORT).show();
}
}
}
//定义广播,用来接收短信,
class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");//提取短信消息
SmsMessage[] messages = new SmsMessage[pdus.length];
Log.e("接收短信的数据1", "===" + messages);
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
//获取发送方号码
String address = messages[0].getOriginatingAddress();
Log.e("接收短信的数据2", "===" + address);
String fullssage = "";
//获取短信内容
for (SmsMessage message : messages) {
fullssage += message.getMessageBody();
}
mTextView_sender.setText(address);
mTextView_content.setText(fullssage);
}
}
}