android读取短信内容 自动填充验证码

public class SmsContent extends ContentObserver {

 
 public static final String SMS_URI_INBOX = "content://sms/inbox";

 private Activity activity = null;

 private String smsContent = "";

 private EditText verifyText = null;

 public SmsContent(Activity activity, Handler handler, EditText verifyText) {
  super(handler);
  this.activity = activity;
  this.verifyText = verifyText;
 }

 @Override
 public void onChange(boolean selfChange) {
  super.onChange(selfChange);
  Cursor cursor = null;// 光标
  // 读取收件箱中指定号码的短信
  cursor = activity.managedQuery(Uri.parse(SMS_URI_INBOX), new String[] { "_id", "address", "body", "read" }, "address=? and read=?",
                                 new String[] { "5554", "0" }, "date desc");

  if (cursor != null) {// 如果短信为未读模式
   cursor.moveToFirst();
   if (cursor.moveToFirst()) {

    String smsbody = cursor.getString(cursor.getColumnIndex("body"));
    System.out.println("smsbody=======================" + smsbody);
    String regEx = "[^0-9]";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(smsbody.toString());
    smsContent = m.replaceAll("").trim().toString();
    verifyText.setText(smsContent);
   }

  }

 }

}

 

掉用该方法的地方的代码:

 

SmsContent content = new SmsContent(LoginActivity.this, new Handler(), verifyText);
  // 注册短信变化监听
  this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);

你可能感兴趣的:(android)