Demo
AndroidManifest.xml 短信广播权限
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/phone"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:maxLength="11"
android:hint="***"
/>
<LinearLayout
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff">
<Button
android:textColor="#fff"
android:background="#F8D775"
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="开启监听"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button
android:id="@+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="小米手机开启短信权限(读取短信/通知短信)"/>
</LinearLayout>
<Button
android:id="@+id/button_exit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="关闭退出"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="监听短信:"/>
<TextView
android:id="@+id/sms_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
public class SMSReceiver extends BroadcastReceiver {
private static MessageListener mMessageListener;
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
public SMSBroadcastReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for(Object pdu:pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte [])pdu);
String sender = smsMessage.getDisplayOriginatingAddress();
//短信内容
String content = smsMessage.getDisplayMessageBody();
String contentFormat="电话号码:" + sender + "\n内容:" + content+"\n------华丽的分割线-----\n";
if (content.contains("关键字")) {
//从短信内容中获取验证码
String code=getCode(content);
//回调数据
mMessageListener.onReceived(contentFormat,code);
}
}
}
}
//回调接口
public interface MessageListener {
public void onReceived(String message,String code);
}
public void setOnReceivedMessageListener(MessageListener messageListener) {
this.mMessageListener = messageListener;
}
public static String getCode(String body) {
String str = Pattern.compile("[^0-9]").matcher(body).replaceAll("");
return str;
}
}
`
注册广播监听
private void initSmsReader(){
//生成广播处理
smsReceiver = new smsReceiver();
//实例化过滤器并设置要过滤的广播
IntentFilter intentFilter = new IntentFilter(SMS_RECEIVED_ACTION);
//设置优先级
intentFilter.setPriority(Integer.MAX_VALUE);
//注册广播
this.registerReceiver(smsReceiver, intentFilter);
mSMSBroadcastReceiver.setOnReceivedMessageListener(new SMSReceiver.MessageListener() {
//监听回调
@Override
public void onReceived(String message,String code) {
//验证码
String oldText = smsText.getText().toString();
smsText.setText(message+oldText);
if(null!=code&& null!=phone){
// TODO
}
}
});
}
//关闭
private void exit(){
if(smsReceiver!=null){
System.out.println("关闭监听");
//注销短信监听广播
this.unregisterReceiver(smsReceiver);
mSMSBroadcastReceiver=null;
}
System.exit(0);
}
Q.E.D.