Android自動讀取簡訊驗證碼,android簡訊驗證碼

來源:互聯網
上載者:User

Android自動讀取簡訊驗證碼,android簡訊驗證碼

實現自動擷取手機的簡訊驗證碼,原理通過監聽簡訊資料庫的變化來解析簡訊,擷取驗證碼。

直接附上代碼:

1.建立一個監聽資料庫的類

import java.util.regex.Matcher;import java.util.regex.Pattern;import android.app.Activity;import android.database.ContentObserver;import android.database.Cursor;import android.net.Uri;import android.os.Handler;import android.widget.EditText;/** * 監聽簡訊資料庫,自動擷取簡訊驗證碼 *  * @author 賈亞光 * @date 2015-3-25 * */public class AutoGetCode extends ContentObserver {    private Cursor cursor = null;    private Activity activity;    private String smsContent = "";    private EditText editText = null;    public AutoGetCode(Activity activity, Handler handler, EditText edittext) {        super(handler);        this.activity = activity;        this.editText = edittext;    }    @Override    public void onChange(boolean selfChange) {        super.onChange(selfChange);        // 讀取收件匣中指定號碼的簡訊        cursor = activity.managedQuery(Uri.parse("content://sms/inbox"),                new String[] { "_id", "address", "read", "body" }, "address=? and read=?",                new String[] {"你要截獲的電話號碼", "0" }, "_id desc");        // 按簡訊id排序,如果按date排序的話,修改手機時間後,讀取的簡訊就不準了        if (cursor != null && cursor.getCount() > 0) {            cursor.moveToFirst();            if (cursor.moveToFirst()) {                String smsbody = cursor                        .getString(cursor.getColumnIndex("body"));                String regEx = "(?<![0-9])([0-9]{" + 6 + "})(?![0-9])";                Pattern p = Pattern.compile(regEx);                Matcher m = p.matcher(smsbody.toString());                while (m.find()) {                    smsContent = m.group();                    editText.setText(smsContent);                }            }        }    }}

2.在使用的時候在activity中註冊

AutoGetCode autoGetCode = new AutoGetCode(RegistCode.this, new Handler(),                regist_code);//regist_code 要顯示驗證碼的EditText        // 註冊簡訊變化監聽        this.getContentResolver().registerContentObserver(                Uri.parse("content://sms/"), true, autoGetCode);

3.在合適的地方取消註冊

@Override    protected void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        this.getContentResolver().unregisterContentObserver(autoGetCode);    }

4.最後別忘記了添加許可權

 <!-- 讀寫簡訊的許可權 -->    <uses-permission android:name="android.permission.RECEIVE_SMS" />    <uses-permission android:name="android.permission.READ_SMS" />    <uses-permission android:name="android.permission.SEND_SMS" />    <uses-permission android:name="android.permission.WRITE_SMS" />

這樣就可以了。

參考資料:

http://blog.sina.com.cn/s/blog_87dc03ea0101dvus.html

http://www.it165.net/pro/html/201406/15300.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.