关于短信接收处理方面,当前已经有一些app做的比较好了,比如发给手机发验证码验证的问题,很多app在手机接收到验证码后,不需要输入,就直接可以跳过验证界面,这就是用到了对接收到的短信的处理。至于短信的发送,也没什么好说的了。在此也只是附上一个小实例。
效果图:
mainactivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
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.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
import android.widget.toast;
public class mainactivity extends activity {
private textview sender;
private textview content;
private intentfilter receivefilter;
private messagereceiver messagereceiver;
private edittext to;
private edittext msginput;
private button send;
private intentfilter sendfilter;
private sendstatusreceiver sendstatusreceiver;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
sender = (textview) findviewbyid(r.id.sender);
content = (textview) findviewbyid(r.id.content);
to = (edittext) findviewbyid(r.id.to);
msginput = (edittext) findviewbyid(r.id.msg_input);
send = (button) findviewbyid(r.id.send);
//为接收短信设置要监听的广播
receivefilter = new intentfilter();
receivefilter.addaction( "android.provider.telephony.sms_received" );
messagereceiver = new messagereceiver();
registerreceiver(messagereceiver, receivefilter);
//为发送短信设置要监听的广播
sendfilter = new intentfilter();
sendfilter.addaction( "sent_sms_action" );
sendstatusreceiver = new sendstatusreceiver();
registerreceiver(sendstatusreceiver, sendfilter);
send.setonclicklistener( new onclicklistener() {
@override
public void onclick(view v) {
//发送短信
//并使用sendtextmessage的第四个参数对短信的发送状态进行监控
smsmanager smsmanager = smsmanager.getdefault();
intent sentintent = new intent( "sent_sms_action" );
pendingintent pi = pendingintent.getbroadcast(
mainactivity. this , 0 , sentintent, 0 );
smsmanager.sendtextmessage(to.gettext().tostring(), null ,
msginput.gettext().tostring(), pi, null );
}
});
}
@override
protected void ondestroy() {
super .ondestroy();
//在activity摧毁的时候停止监听
unregisterreceiver(messagereceiver);
unregisterreceiver(sendstatusreceiver);
}
class messagereceiver extends broadcastreceiver {
@override
public void onreceive(context context, intent intent) {
bundle bundle = intent.getextras();
//使用pdu秘钥来提取一个pdus数组
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();
}
sender.settext(address);
content.settext(fullmessage);
}
}
class sendstatusreceiver extends broadcastreceiver {
@override
public void onreceive(context context, intent intent) {
if (getresultcode() == result_ok) {
//发送成功
toast.maketext(context, "send succeeded" , toast.length_long)
.show();
} else {
//发送失败
toast.maketext(context, "send failed" , toast.length_long)
.show();
}
}
}
}
|
activity_main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "50dp" >
<textview
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:padding= "10dp"
android:text= "from:" />
<textview
android:id= "@+id/sender"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical" />
</linearlayout>
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "50dp" >
<textview
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:padding= "10dp"
android:text= "content:" />
<textview
android:id= "@+id/content"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical" />
</linearlayout>
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "50dp" >
<textview
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:padding= "10dp"
android:text= "to:" />
<edittext
android:id= "@+id/to"
android:layout_width= "0dp"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:layout_weight= "1" />
</linearlayout>
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "50dp" >
<edittext
android:id= "@+id/msg_input"
android:layout_width= "0dp"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:layout_weight= "1" />
<button
android:id= "@+id/send"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:text= "send" />
</linearlayout>
</linearlayout>
|
androidmanifest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package = "com.example.smstest"
android:versioncode= "1"
android:versionname= "1.0" >
<uses-sdk
android:minsdkversion= "14"
android:targetsdkversion= "17" />
//接受短信
<uses-permission android:name= "android.permission.receive_sms" />
//发送短信
<uses-permission android:name= "android.permission.send_sms" />
<application
android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
android:label= "@string/app_name"
android:theme= "@style/apptheme" >
<activity
android:name= "com.example.smstest.mainactivity"
android:label= "@string/app_name" >
<intent-filter>
<action android:name= "android.intent.action.main" />
<category android:name= "android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>
|
以上就是本文的全部内容,希望对大家的学习有所帮助。