时间:2021-08-21 09:10:50 | 栏目:Android代码 | 点击:次
本文实例为大家分享了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>