Android如何实现接收和发送短信
程序员文章站
2024-03-06 19:37:56
每一部手机都具有短信接收和发送功能,下面我们通过代码来实现接收和发送短信功能。
一、接收短信
1、创建内部广播接收器类,接收系统发出的短信广播
2、...
每一部手机都具有短信接收和发送功能,下面我们通过代码来实现接收和发送短信功能。
一、接收短信
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"/>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。