知識準備:
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>
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。