android:name="android.permission.SEND_SMS"android:name="android.permission.READ_SMS"android:name="android.permission.RECEIVE_SMS"自动填写短信验证码[图片上传中...(image-f5bada-1527212844994-2)]以下代码测试环境为:Android模拟器:5.0(因为模拟器发送短信方便快捷)Android Studio:2.2.2代码主要借鉴于这里注册短信广播分为动态注册(即Java代码中register)和静态注册(即配置文件Manifest.xml中配置receiver)动态注册广播privatevoidregistSmsReciver(){ IntentFilter filter =newIntentFilter(); filter.addAction("android.provider.Telephony.SMS_RECEIVED");// 设置优先级 不然监听不到短信filter.setPriority(1000); Snackbar.make(editText,"注册短信广播", Snackbar.LENGTH_LONG).show(); Log.d("TAG","registSmsReciver "); registerReceiver(smsReciver, filter); }静态注册广播android.provider.Telephony.SMS_RECEIVED"短信广播publicclassSmsReciverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Context context, Intent intent){ Object[] objs = (Object[]) intent.getExtras().get("pdus");for(Object obj : objs) {byte[] pdu = (byte[]) obj; SmsMessage sms = SmsMessage.createFromPdu(pdu);// 短信的内容String message = sms.getMessageBody(); Log.d("TAG","message="+ message);// 短信的发送方String from = sms.getOriginatingAddress(); Log.d("TAG","from="+ from); analysisVerify(message); } }/** * 解析短信并且回写,主要是提取出数字验证码并显示在输入框上 * *@parammessage */privatevoidanalysisVerify(String message){char[] msgs = message.toCharArray(); StringBuffer sb =newStringBuffer();for(inti =0; i < msgs.length; i++) {if('0'<= msgs[i] && msgs[i] <='9') { sb.append(msgs[i]); } } editText.setText(sb.toString()); } }取消注册广播@OverrideprotectedvoidonDestroy(){super.onDestroy();// 取消短信广播注册if(smsReciver !=null) { unregisterReceiver(smsReciver); smsReciver =null; } }publicStringgetSmsInPhone(){finalString SMS_URI_ALL ="content://sms/";finalString SMS_URI_INBOX ="content://sms/inbox";finalString SMS_URI_SEND ="content://sms/sent";finalString SMS_URI_DRAFT ="content://sms/draft";finalString SMS_URI_OUTBOX ="content://sms/outbox";finalString SMS_URI_FAILED ="content://sms/failed";finalString SMS_URI_QUEUED ="content://sms/queued"; StringBuilder smsBuilder =newStringBuilder();try{ Uri uri = Uri.parse(SMS_URI_INBOX); String[] projection =newString[] {"_id","address","person","body","date","type"}; Cursor cur = getContentResolver().query(uri, projection,null,null,"date desc");// 获取手机内部短信if(cur.moveToFirst()) {intindex_Address = cur.getColumnIndex("address");intindex_Person = cur.getColumnIndex("person");intindex_Body = cur.getColumnIndex("body");intindex_Date = cur.getColumnIndex("date");intindex_Type = cur.getColumnIndex("type");do{ String strAddress = cur.getString(index_Address);intintPerson = cur.getInt(index_Person); String strbody = cur.getString(index_Body);longlongDate = cur.getLong(index_Date);intintType = cur.getInt(index_Type); SimpleDateFormat dateFormat =newSimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d =newDate(longDate); String strDate = dateFormat.format(d); String strType ="";if(intType ==1) { strType ="接收"; }elseif(intType ==2) { strType ="发送"; }else{ strType ="null"; } smsBuilder.append("[ "); smsBuilder.append(strAddress +", "); smsBuilder.append(intPerson +", "); smsBuilder.append(strbody +", "); smsBuilder.append(strDate +", "); smsBuilder.append(strType); smsBuilder.append(" ]\n\n"); }while(cur.moveToNext());if(!cur.isClosed()) { cur.close(); cur =null; } }else{ smsBuilder.append("no result!"); }// end ifsmsBuilder.append("getSmsInPhone has executed!"); }catch(SQLiteException ex) { LogUtil.d("TAG", ex.getMessage()); }returnsmsBuilder.toString(); }运行时权限google在6.0以后加入了运行时权限,就是说有些危险权限是在触发这些相关操作时会弹出对话框让用户来选择是否允许,比如打电话或者发短信这类涉及到资费的权限等。危险权限包括以下几大分类:1,日历:读取,写入2,摄像头3,联系人:读取,写入,获取帐户4,位置5,录音6,手机状态:拨打电话,读取通话记录等7,传感器8,短信9,存储卡读写至于怎么改,以下代码来源于第一行代码第二版,郭神在12.27号的直播中又提供了三种更好的方法,大家可以参考直播视频回放首先判断用户是否已经对该权限授权过,没有则弹框提示,有的话直接进行操if(ContextCompat.checkSelfPermission(SmsCodeActivity.this, android.Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(SmsCodeActivity.this,newString[]{Manifest.permission.RECEIVE_SMS},1); }else{// 原来的敏感操作代码:发短信或者收短信}然后就是对弹框操作的回调@OverridepublicvoidonRequestPermissionsResult(intrequestCode, String[] permissions,int[] grantResults){super.onRequestPermissionsResult(requestCode, permissions, grantResults);switch(requestCode) {case1:if(grantResults.length >0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {// 原来的敏感操作代码:发短信或者收短信}else{ Snackbar.make(editText,"你拒绝了该权限", Snackbar.LENGTH_LONG).show(); }break; } }获取本机号码通用方法一,此方法的前提是相应运营商的sim卡上必须存着号码才行,否则只能取空:TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);Stringnumber = tm.getLine1Number();StringsimSerialNumber = tm.getSimSerialNumber();Stringimei = tm.getDeviceId(); Log.i("TAG","n"+number+"s"+simSerialNumber+"i"+imei);不靠谱方法二:可通过facebook api或者whatsapp api等类似的api可以获取不靠谱方法三:偷偷发短信给10086或者10010然后拦截短信截取其中的本机号码
BroadcastReceiver广播收到信息,验证码截取
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 短信权限 自动填写短信验证码 [图片上传中...(image-f5bada-1527212844994-2)] 以...
- 我想几乎所有的安卓开发者,第一个接触到的四大组件之一就是activity,而我在之前的文章中也写过, 在activ...