作者: 牙叔
现在手机系统非常安全,除非经过用户同意,否则任何软件都无法获取短信
使用情景
安卓开发:获取验证码
警告
本教程仅供学习, 严禁用于非法用途
三种方法
- 广播监听: BroadcastReceiver
- 通知栏监听: events.onNotification
- 内容观察者: ContentObserver
三种方法的优缺点
- 广播监听: 我的小米8手机获取不到这个广播, 群里有的人说可以, 有的人说不行
- 通知栏监听: events.onNotification, 这个需要通知栏权限, 短信内容可能在
通知文本
也可能在通知摘要
中 - 内容观察者: ContentObserver, 这个需要读取短信的权限, 小米短信分两种: 普通短信, 通知类短信
总结
- 从我自己的测试结果来看, 我推荐第三种
- 第一种广播监听, 我的手机根本收不到广播
- 第二种通知栏监听, 需要跳转到通知栏, 然后查找atuojs所在位置, 再点击允许, 至少要点击2次, 再加一次肉眼寻找app
- 第三种, 效果综合最好
具体代码
-
广播监听
importPackage(android.content); importClass(android.telephony.SmsMessage); IntentFilter = android.content.IntentFilter; let receiver = new BroadcastReceiver(function (ctx, intent) { var sender = null; var bundle = intent.getExtras(); var format = intent.getStringExtra("format"); if (bundle != null) { var pdus = bundle.get("pdus"); for (object in pdus) { var message = SmsMessage.createFromPdu(pdus[object], format); sender = message.getOriginatingAddress(); messageBody = message.getMessageBody(); log("发信人: " + replacepos(sender, 6, 9, "****")); log("短信内容: " + messageBody); } } }); let action = "android.provider.Telephony.SMS_RECEIVED"; context.registerReceiver(receiver, new IntentFilter(action)); events.on("exit", () => { context.unregisterReceiver(receiver); }); setTimeout(() => { }, 60000); function replacepos(text, start, stop, replacetext) { mystr = text.substring(0, start) + replacetext + text.substring(stop + 1); return mystr; }
-
通知栏监听
events.observeNotification(); events.onNotification(function(notification){ printNotification(notification); }); toast("监听中,请在日志中查看记录的通知及其内容"); function printNotification(notification){ log("应用包名: " + notification.getPackageName()); log("通知文本: " + notification.getText()); log("通知优先级: " + notification.priority); log("通知目录: " + notification.category); log("通知时间: " + new Date(notification.when)); log("通知数: " + notification.number); log("通知摘要: " + notification.tickerText); }
-
内容观察者
公众号回复: 短信监听, 获取内容观察者的详细代码
微信公众号: AutoJsPro教程
QQ交流群
747748653