Android如何实现接收和发送短信

每一部手机都具有短信接收和发送功能,下面我们通过代码来实现接收和发送短信功能。

一、接收短信

1、创建内部广播接收器类,接收系统发出的短信广播
2、从获得的内容中解析出短信发送者和短信内容
3、在Activity中注册广播
4、添加接收短信权限

下面放上具体的代码 
activity_main.xml文件用于显示短信发送者号码和显示短信内容

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/sms_from" android:layout_width="wrap_content" android:layout_height="20dp" android:text="From" /> <TextView android:id="@+id/sms_from_txt" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/sms_from"/> <TextView android:id="@+id/sms_content" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_below="@id/sms_from" android:layout_marginTop="20dp" android:text="Content" /> <TextView android:id="@+id/sms_content_txt" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="15dp" android:layout_marginTop="20dp" android:layout_below="@id/sms_from_txt" android:layout_toRightOf="@id/sms_content"/> </RelativeLayout>

MainActivity.java文件

public class MainActivity extends AppCompatActivity { private TextView fromTv; private TextView contentTv; private IntentFilter intentFilter; private MessageReceiver messageReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); getSms(); } private void getSms() { intentFilter = new IntentFilter(); intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); messageReceiver = new MessageReceiver(); //设置较高的优先级 intentFilter.setPriority(100); registerReceiver(messageReceiver, intentFilter); } private void initView() { fromTv = (TextView) findViewById(R.id.sms_from_txt); contentTv = (TextView) findViewById(R.id.sms_content_txt); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(messageReceiver); } 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]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } //获取发送方号码 String address = messages[0].getOriginatingAddress(); String fullMessage = ""; for (SmsMessage message : messages) { //获取短信内容 fullMessage += message.getMessageBody(); } //截断广播,阻止其继续被Android自带的短信程序接收到 abortBroadcast(); fromTv.setText(address); contentTv.setText(fullMessage); } } }

注:注册的广播接收器,一定要在OnDestroy()方法中取消注册。

由于短信广播是有序广播,如果我们不想让Android自带的短信程序接收到短信,就可以设置我们自身接收器的优先级,同时在我们接受完广播后将广播截断,阻止其被Android自带的短信程序接收到。

二、发送短信

1、获取接收者的号码和短信内容
2、获得短信发送管理实例
3、构造PendingIntent启动短信发送状态监控广播
4、调用发送短信函数,传入参数发送短信
5、构造广播接收器内部类监控短信是否发送成功
6、获得广播接收器实例和IntentFilter实例,注册广播接收器
7、在onDestroy()中取消注册的广播接收器
8、在AndroidManifest.xml文件中加入短信发送权限

下面放上具体的布局文件和代码 
activity_send_msg.xml文件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/to_ed" android:layout_width="match_parent" android:layout_height="50dp" android:hint="to"/> <EditText android:id="@+id/to_content" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/to_ed" android:hint="content"/> <Button android:id="@+id/send_msg" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/to_content" android:text="@string/send_message"/> </RelativeLayout>

SendMsgActivity.java文件

public class SendMsgActivity extends AppCompatActivity implements View.OnClickListener { private Context context; private EditText toEdit; private EditText toContent; private IntentFilter sendFilter; private SendStatusReceiver sendStatusReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_msg); context = this; initView(); } private void initView() { toEdit = (EditText) findViewById(R.id.to_ed); toContent = (EditText) findViewById(R.id.to_content); //添加发送按钮的点击监听事件 Button sendMsg = (Button) findViewById(R.id.send_msg); sendMsg.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.send_msg: sendMessage(); break; default: break; } } private void sendMessage() { //获取短信接收者号码 String to = toEdit.getText().toString(); //获取发送短信内容 String content = toContent.getText().toString(); //获得广播接收器实例和IntentFilter实例 sendStatusReceiver = new SendStatusReceiver(); sendFilter = new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); //注册广播监听 registerReceiver(sendStatusReceiver, sendFilter); //构造PendingIntent启动短信发送状态监控广播 Intent sendIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast(context, 0, sendIntent, 0); //获得短信管理实例 SmsManager smsManager = SmsManager.getDefault(); //调用发送短信函数,传入参数发送短信(第一、三、四参数依次为接收者号码,短信内容,短信发送状态监控的PendingIntent) smsManager.sendTextMessage(to, null, content, pi, null); } /** * 构造广播接收器内部类监控短信是否发送成功 */ class SendStatusReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if (getResultCode() == RESULT_OK){ Toast.makeText(context, "successful", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(context, "failed", Toast.LENGTH_SHORT).show(); } } } @Override protected void onDestroy() { super.onDestroy(); //取消注册的广播 unregisterReceiver(sendStatusReceiver); } }

在AndroidManifest.xml文件中加入短信发送权限 
<uses-permission android:name="android.permission.SEND_SMS"/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-07-30 23:42:30

Android如何实现接收和发送短信的相关文章

Android接收和发送短信的实现代码_Android

Android收到短信时会广播android.provider.Telephony.SMS_RECEIVED消息,因此只要定义一个Receiver,收听该消息,就能接收短信. <receiver android:name=".smsReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </in

Android接收和发送短信处理_Android

关于短信接收处理方面,当前已经有一些app做的比较好了,比如发给手机发验证码验证的问题,很多app在手机接收到验证码后,不需要输入,就直接可以跳过验证界面,这就是用到了对接收到的短信的处理.至于短信的发送,也没什么好说的了.在此也只是附上一个小实例. 效果图: MainActivity: import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver;

Android接收和发送短信的实现代码

Android收到短信时会广播android.provider.Telephony.SMS_RECEIVED消息,因此只要定义一个Receiver,收听该消息,就能接收短信. <receiver android:name=".smsReceiver" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </in

Android项目实现短信的发送、接收和对短信进行拦截_Android

说实话,关于Android中对短信的一些相关操作是一个比较入门的东西.那我现在还要来写这一篇博客的原因只是因为现在开发中有相关内容,而又想将这些东西分享给更多的人来学习,同时在以后对Android系统的短信进行其他学习的时候也就放在这里做一个记录了,于是就写了这篇啰嗦的文章.如果你觉得这是一个不错的东西,欢迎收藏,以便在以后更方便地查看本人在此篇文章中更新的内容.下面我就从标题中的三个方面来对Android系统中的短信操作进行一个简单地学习. 短信的发送 由于Android中对短信发送方法的优良

Android项目实现短信的发送、接收和对短信进行拦截

说实话,关于Android中对短信的一些相关操作是一个比较入门的东西.那我现在还要来写这一篇博客的原因只是因为现在开发中有相关内容,而又想将这些东西分享给更多的人来学习,同时在以后对Android系统的短信进行其他学习的时候也就放在这里做一个记录了,于是就写了这篇啰嗦的文章.如果你觉得这是一个不错的东西,欢迎收藏,以便在以后更方便地查看本人在此篇文章中更新的内容.下面我就从标题中的三个方面来对Android系统中的短信操作进行一个简单地学习. 短信的发送 由于Android中对短信发送方法的优良

broadcastreceiver-自己做了一个接收发送短信的练习,但是一点发送按钮就报错,也接收不了信息。求大神

问题描述 自己做了一个接收发送短信的练习,但是一点发送按钮就报错,也接收不了信息.求大神 帮帮小弟,良辰必有重谢!!!targetSDK23,minSDK18 package com.example.think.smstest; import android.annotation.TargetApi; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content

Android开发中实现发送短信的小程序示例_Android

上图为代码结构图. 现在我们看下具体的代码. Send.java package cn.com.sms.send; import java.util.ArrayList; import java.util.Iterator; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.te

Android发送短信方法实例详解_Android

本文实例讲述了Android发送短信方法.分享给大家供大家参考,具体如下: 短信和打电话一样,都是android手机的基本功能,下面以实例说明android如何实现发送短信的功能. 程序如下所示: import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.app.PendingIntent; import android.content.I

Android发送短信方法总结_Android

android API 中提供了SmsManager类处理短信.其中的sendTextMessage(num, null, content, pend, null)函数就是发送,具体介绍如下: SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员.公有方法: 1.ArrayList<String> divideMessage(String text) 当短信超过SMS消息的最大长度时,将短