本文实例讲解了android自动提取短信验证码解决方案,分享给大家供大家参考,具体内容如下
1
2
|
<uses-permission android:name= "android.permission.receive_sms" ></uses-permission> <uses-permission android:name= "android.permission.read_sms" ></uses-permission> |
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
|
package com.dongdongsongcan.app; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.smsmessage; import com.su.dolphin.utils.logutil; import java.util.arraylist; import java.util.list; import java.util.regex.matcher; import java.util.regex.pattern; public class smsreceiver extends broadcastreceiver { public interface ismslistener { public void onsmsreceive(string verifycode); } private static ismslistener msmslistener; public smsreceiver(ismslistener ismslistener) { msmslistener = ismslistener; } public static final string tag = "imichatsmsreceiver" ; //android.provider.telephony.sms.intents public static final string sms_received_action = "android.provider.telephony.sms_received" ; @override public void onreceive(context context, intent intent) { logutil.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ); if (intent.getaction().equals(sms_received_action)) { smsmessage[] messages = getmessagesfromintent(intent); for (smsmessage message : messages) { // logutil.d(message.getoriginatingaddress() + " : " + // // message.getdisplayoriginatingaddress() + " : " + // // message.getdisplaymessagebody() + " : " + // // message.gettimestampmillis()); string msg = message.getdisplaymessagebody(); logutil.d( "msg: " + msg); string verifycode = null ; pattern p = pattern.compile( "\\d{4}" ); matcher m = p.matcher(msg); while (m.find()) { verifycode = m.group(); break ; } logutil.d( "verifycode " + verifycode); if (msmslistener != null ) { msmslistener.onsmsreceive(verifycode); } } } } public final smsmessage[] getmessagesfromintent(intent intent) { object[] messages = (object[]) intent.getserializableextra( "pdus" ); byte [][] pduobjs = new byte [messages.length][]; for ( int i = 0 ; i < messages.length; i++) { pduobjs[i] = ( byte []) messages[i]; } byte [][] pdus = new byte [pduobjs.length][]; int pducount = pdus.length; smsmessage[] msgs = new smsmessage[pducount]; for ( int i = 0 ; i < pducount; i++) { pdus[i] = pduobjs[i]; msgs[i] = smsmessage.createfrompdu(pdus[i]); } return msgs; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助。