Android实现短信验证码自动拦截读取功能
程序员文章站
2024-03-06 18:09:08
本文实例为大家分享了android短信验证码自动拦截读取 的具体代码,供大家参考,具体内容如下
知识准备:
1.观察者模式的理解【文章稍后来到~~】...
本文实例为大家分享了android短信验证码自动拦截读取 的具体代码,供大家参考,具体内容如下
知识准备:
1.观察者模式的理解【文章稍后来到~~】
2.android的cursor使用【android基础】
3.正则表达式使用【java基础】
4.handler使用【android基础】
代码整理:
mainactivity.java
import android.net.uri; import android.os.bundle; import android.os.handler; import android.os.message; import android.support.v7.app.appcompatactivity; import android.view.menu; import android.view.menuitem; import android.widget.edittext; public class mainactivity extends appcompatactivity { public static final int msg_receiver_code = 1; private edittext smset; private handler handler = new handler() { @override public void handlemessage(message msg) { switch (msg.what) { case msg_receiver_code: string message= (string) msg.obj; smset.settext(message); smset.setselection(message.length()); break; default: break; } } }; private mysmsobserver mysmsobserver; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); smset = (edittext) findviewbyid(r.id.smset); mysmsobserver = new mysmsobserver(this, handler); uri uri=uri.parse("content://sms"); getcontentresolver().registercontentobserver(uri,true, mysmsobserver); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override protected void ondestroy() { super.ondestroy(); getcontentresolver().unregistercontentobserver(mysmsobserver); } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
mysmsobserver.java
import android.content.context; import android.database.contentobserver; import android.database.cursor; import android.net.uri; import android.os.handler; import android.util.log; import java.util.regex.matcher; import java.util.regex.pattern; /** * created by nate on 2015/9/28. */ public class mysmsobserver extends contentobserver { private context mcontext; private handler mhandler; public mysmsobserver(context context, handler handler) { super(handler); this.mcontext = context; this.mhandler = handler; } @override public void onchange(boolean selfchange, uri uri) { super.onchange(selfchange, uri); if (uri.tostring().equals("content://sms/raw")) { return; } uri queryuri = uri.parse("content://sms/inbox"); string code = ""; cursor cursor = mcontext.getcontentresolver().query(queryuri, null, null, null, "date desc"); if (cursor != null) { if (cursor.movetofirst()) { string address = cursor.getstring(cursor.getcolumnindex("address")); string message = cursor.getstring(cursor.getcolumnindex("body")); // todo: 2015/9/28 这里可以根据address做一些自己的判断,比如只有特定的手机号才做判断 log.e("guxuewu", "address:==>" + address + " message:==>" + message); // todo: 2015/9/28 这里可以根据自己的项目进行特定的正则表达式的编写 pattern pattern = pattern.compile("(\\d{6})"); matcher matcher = pattern.matcher(message); if (matcher.find()) { code = matcher.group(0); mhandler.obtainmessage(mainactivity.msg_receiver_code, code).sendtotarget(); } } cursor.close(); } } }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <edittext android:id="@+id/smset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:ems="10" /> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。