Android實現簡訊驗證碼自動攔截讀取功能_Android

來源:互聯網
上載者:User

知識準備:
 1.觀察者模式的理解【文章稍後來到~~】
 2.Android的Cursor使用【Android基礎】
 3.Regex使用【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;@Overrideprotected 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);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {  getMenuInflater().inflate(R.menu.menu_main, menu);  return true;}@Overrideprotected void onDestroy() {  super.onDestroy();  getContentResolver().unregisterContentObserver(mySmsObserver);}@Overridepublic 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;}@Overridepublic 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 這裡可以根據自己的項目進行特定的Regex的編寫       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>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.