首先这个东西是这样的流程:
不管你用mob还是移动还是自己的短信接口都一样的,在发送验证码的部分去请求,然后服务器返回给你短信息,然后你自己处理,达到效果。
这样一说就是只需要去监听短信息的数据库变化就可以了,并且要监听到你想要的那一条。
首先是
ContentObserver
import android.annotation.SuppressLint; import android.content.Context; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Handler; import android.provider.Settings; import android.util.Log; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SmsObserver extends ContentObserver { private Context mContext; private Handler mHandler; public SmsObserver(Context context, Handler handler) { super(handler); mContext = context; mHandler = handler; } @SuppressLint("NewApi") @Override public void onChange(boolean selfChange, Uri uri) { super.onChange(selfChange, uri); Log.e("DEBUG", "SMS has changed!"); Log.e("DEBUG", uri.toString()); // try { // int isAirplaneOpen = Settings.System.getInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); // Log.i("奇怪了", " isAirplaneOpen -----> " + isAirplaneOpen); // mHandler.obtainMessage(LoginActivity.MSG_RECEIVED_CODE, isAirplaneOpen) // .sendToTarget(); // } catch (Settings.SettingNotFoundException e) { // e.printStackTrace(); // } String code = ""; if (uri.toString().equals("content://sms/raw")) { return; } String[] projection = new String[]{"_id", "address", "body", "type"}; Uri inboxUri = Uri.parse("content://sms/inbox"); Cursor c = mContext.getContentResolver().query(inboxUri, projection, null, null, "date desc"); if (c != null) { if (c.moveToFirst()) { int _id = c.getInt(c.getColumnIndex("_id")); String type = c.getString(c.getColumnIndex("type")); String address = c.getString(c.getColumnIndex("address")); String body = c.getString(c.getColumnIndex("body")); Log.e("--------address------", address); Log.v("-----草-----", "msgId : " + _id); Log.v("-----草-----", "msgAddr : " + type); Log.v("-----草-----", "msgBody : " + body); Log.v("-----草-----", "msgType : " + address); if (!address.contains("10657120610111")) { return;//10657120610111 } Log.e("DEBUG", "发件人为:" + address + " " + "短信内容为:" + body); Pattern pattern = Pattern.compile("(\\d{6})"); Matcher matcher = pattern.matcher(body); if (matcher.find()) { code = matcher.group(0); Log.e("DEBUG", "code is " + code); mHandler.obtainMessage(LoginActivity.MSG_RECEIVED_CODE, code) .sendToTarget(); } } c.close(); } } }
这个就是观察者模式的类,作用就是针对短信进行监听,根据其变化来获取短信息的内容,并且根据正则表达式去截取想要的数字,然后handle一下。
然后是
MainActivity
private SmsObserver mObserver; public static final int MSG_RECEIVED_CODE = 1; @SuppressLint("HandlerLeak") private Handler mHandler2 = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == MSG_RECEIVED_CODE) { String code = (String) msg.obj; // update the UI inputCodeEt.setText(code); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); init(); mObserver = new SmsObserver(LoginActivity.this, mHandler2); Uri uri = Uri.parse("content://sms"); getContentResolver().registerContentObserver(uri, true, mObserver); } @Override protected void onDestroy() { getContentResolver().unregisterContentObserver(mObserver); super.onDestroy(); }
在main里面进行注册监听,别忘了最后要destroy
这样在监听状态下就会实时的对短信息的收发进行监听,并且得到我们想要的数字,通过子线程更新EditText达到自动填写的效果。
最后总结一下,一般像短信息啦,飞行模式啦什么设置之类的建议用ContentObserver搞,像什么推送啦,更新啦,视频聊天啦,建议用service搞,合理的分配内存使用,优化app的性能。
地址:http://down.51cto.com/data/2103679
时间: 2024-11-05 19:34:44