Last time we talked about the use of content observers. If you are careful, you will notice that you need to actively query to know if the database has changed. What if we want to know the first time the database changes? Here we take reading SMS data as an example to illustrate the use of content observer.

First we need to add a permission

<uses-permission android:name="android.permission.READ_SMS"/>
Copy the code

With this permission we can access the content of the message

Next we will add observation code to the interface that needs to read the text message. The sample code is as follows

public class GcdxnrActivity extends WaterPermissionActivity<GcdxnrModel> implements GcdxnrCallback { private Handler mHandler = new Handler(); // Declare a processor object private SmsGerObserver mObserver; Private static Uri mSmsUri; Private static String[] mSmsColumn; Private TextView TV_chect_flow; @Override protected GcdxnrModel getModelImp() { return new GcdxnrModel(this,this); } @Override protected int getContentLayoutId() { return R.layout.activity_gcdxnr; } @Override protected void initWidget() { tv_chect_flow = findViewById(R.id.tv_chect_flow); requestPermission(READ_SMS); } @Override protected void doSMS() { initSmsObserver(); } private void initSmsObserver(){mSmsUri = uri.parse ("content:// SMS "); mSmsColumn = new String[]{"address","body","date"}; // Create an SMS observer object mObserver = new SmsGerObserver(this,mHandler); // Register a content observer for the specified Uri. As soon as data changes within the Uri, the observer's onChange method getContentResolver().registercontentobServer (mSmsUri,true,mObserver) is started; } // Override protected void onDestroy() {// Cancel the content viewer getContentResolver().unregisterContentObserver(mObserver); super.onDestroy(); } private static class SmsGerObserver extends ContentObserver {private Activity mActivity; // Declare a context object private GcdxnrActivity mainActivity; public SmsGerObserver(Activity activity, Handler handler){ super(handler); mActivity = activity; mainActivity = (GcdxnrActivity) activity; } public void onChange(boolean selfChange){ String sender = ""; String content = ""; // Build a conditional statement to query SMS messages. So find 10086 text message String selection = the String. Format (" address = '1008611' and the date > % d ", System. CurrentTimeMillis () - 1000 * 60); / / through content parser to obtain eligible result set Cursor Cursor Cursor. = mActivity getContentResolver () query (mSmsUri mSmsColumn, selection, null, "the date desc"); While (cursor.movetonext ()){sender = cursor.getString(0); content = cursor.getString(1); break; } cursor.close(); // Close the database cursor mainActivity.settext (" sender: "+sender+"\n"+" content: "+content); super.onChange(selfChange); } } public void setText(String text){ if (tv_chect_flow! =null) { tv_chect_flow.setText(text); }}}Copy the code

First of all, we can see

requestPermission(READ_SMS);
Copy the code

Here a manifest.permission.read_sms dynamic permission is requested, and doSMS() is the method executed after the user agrees to the permission. Here we execute the initSmsObserver() method, which initializes the observer. You can use your own dynamic permission request method.

If the database changes, the onChange method is called. Sender is the sender of the SMS, and content is the content of the SMS.

String selection = String.format("address='1008611' and date>%d",System.currentTimeMillis() - 1000*60);
Copy the code

This code specifies the number from which the message to be queried was sent, and how long ago the message to be queried.

Let me know in the comments section if you have any questions.