这篇文章给大家介绍使用Android怎么监听手机短信,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
前提--权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" > </uses-permission> <uses-permission android:name="android.permission.READ_SMS" > </uses-permission>
定义短信广播接收者
public class SMSBroadcastReceiverextends BroadcastReceiver{ private static OnReceivedMessageListener mOnReceivedMessageListener; 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(); long date = smsMessage.getTimestampMillis(); Date tiemDate = new Date(date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = simpleDateFormat.format(tiemDate); //过滤不需要读取的短信的发送号码 if ("106902780116481".equals(sender)) { mOnReceivedMessageListener.onReceived(getDynamicPassword(content)); abortBroadcast(); } } } } public interface OnReceivedMessageListener{ void onReceived(String message); } public void setOnReceivedMessageListener(OnReceivedMessageListener onReceivedMessageListener){ this.mOnReceivedMessageListener = onReceivedMessageListener; } /** * 从字符串中截取连续6位数字组合 ([0-9]{" + 6 + "})截取六位数字 进行前后断言不能出现数字 用于从短信中获取动态密码 * @param str 短信内容 * @return 截取得到的6位动态密码 */ public String getDynamicPassword(String str){ // 6是验证码的位数,一般为六位 Pattern continuousNumberPattern = Pattern.compile("(?<![0-9])([0-9]{" + 6 + "})(?![0-9])"); Matcher m = continuousNumberPattern.matcher(str); String dynamicPassword = ""; while (m.find()) { dynamicPassword = m.group(); } return dynamicPassword; } }
注册广播
SMSBroadcastReceiver = mSMSBroadcastReceiver = new SMSBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION); intentFilter.setPriority(Integer.MAX_VALUE); this.registerReceiver(mSMSBroadcastReceiver, intentFilter); ToastManager.getInstance().showLongToast("注册短信监听"); mSMSBroadcastReceiver.setOnReceivedMessageListener(new SMSBroadcastReceiver.OnReceivedMessageListener() { @Override public void onReceived(String message){ //do something } });
方式二:监听短信数据库
内容提供者中对外通信方式有3种:
方式1:定义为Activity的内部类,直接在onChang()方法中处理业务逻辑,比如收到短信之后设置给EditText
优点:方便
缺点:耦合度高,复用性差
方式2:定义接口,Activity实现这个接口,拿到回调,处理业务逻辑
优点:耦合度低,复用性高
缺点没用充分利用Handler资源
代码如下:
/** * Created by wxw on 2016/9/24 10:34 * function: 监听短信数据库 * e-mail:wangxw725@163.com */ public class SmsContentObserverextends ContentObserver{ private Cursor cursor = null; private Context mContext; private String[] projection = new String[]{"_id","body"}; private OnMessageObservedListener mOnMessageObservedListener; public SmsContentObserver(Context context,Handler handler){ super(handler); this.mContext = context; } public interface OnMessageObservedListener{ void onObservedMessage(String message); } public void setOnMessageObservedListener(OnMessageObservedListener onMessageObservedListener){ this.mOnMessageObservedListener = onMessageObservedListener; } @Override public void onChange(boolean selfChange){ super.onChange(selfChange); cursor = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), projection,null, null, "_id desc"); if (cursor != null && cursor.getCount() > 0) { cursor.moveToNext(); int smsbodyColumn = cursor.getColumnIndex("body"); String smsBody = cursor.getString(smsbodyColumn); mOnMessageObservedListener.onObservedMessage(getDynamicPassword(smsBody)); } cursor.close(); } private String getDynamicPassword(String str){ Pattern continuousNumberPattern = Pattern.compile("(?<![0-9])([0-9]{" + 6 + "})(?![0-9])"); Matcher m = continuousNumberPattern.matcher(str); String dynamicPassword = ""; while (m.find()) { dynamicPassword = m.group(); } return dynamicPassword; } }
方式3:通过Handler发送消息,在Activity中进行业务逻辑处理
代码如下:
/** * Created by wxw on 2016/9/24 10:34 * function: 监听短信数据库 * e-mail:wangxw725@163.com */ public class SmsContentObserverextends ContentObserver{ private Cursor cursor = null; private Context mContext; private Handler mHandler; private String[] projection = new String[]{"_id","body"}; public SmsContentObserver(Context context,Handler handler){ super(handler); this.mContext = context; this.mHandler = handler; } @Override public void onChange(boolean selfChange){ super.onChange(selfChange); cursor = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), projection,null, null, "_id desc"); if (cursor != null && cursor.getCount() > 0) { cursor.moveToNext(); int smsbodyColumn = cursor.getColumnIndex("body"); String smsBody = cursor.getString(smsbodyColumn); mHandler.obtainMessage(1,getDynamicPassword(smsBody)).sendToTarget(); } cursor.close(); } private String getDynamicPassword(String str){ Pattern continuousNumberPattern = Pattern.compile("(?<![0-9])([0-9]{" + 6 + "})(?![0-9])"); Matcher m = continuousNumberPattern.matcher(str); String dynamicPassword = ""; while (m.find()) { dynamicPassword = m.group(); } return dynamicPassword; } }
注册观察者
smsContentObserver = new SmsContentObserver(this, smsHandler); getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsContentObserver);
handler中处理消息
private Handler smsHandler = new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what){ case 0: ...... break; case 1: LogUtils.x("Activity收到了短息码:"+(String)msg.obj); lf.mEtPassword.setText((String)msg.obj); break; } } };
注销观察者
@Override protected void onDestroy(){ super.onDestroy(); ...... if(smsContentObserver!=null){ getContentResolver().unregisterContentObserver(smsContentObserver); smsContentObserver = null; } }
关于使用Android怎么监听手机短信就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。