Example analysis of listener application in Android programming

  • 2020-12-10 00:51:38
  • OfStack

This paper illustrates the listener method of Android programming. To share for your reference, the details are as follows:

The listener is explained by listening for changes in content provider data and listening for sending text messages. If the user provider data changes, the listener will immediately receive the record of the database operation, and the listener is using the notification mechanism, if not using the notification mechanism can also do, that is, constantly query the database, so the efficiency will be low. With notification, when a user sends a record to the database, ContentObserver gets it right away, and we can process the data.

Listen for changes in content provider data

1. The content provider can notify other programs to listen when the data changes

ContentResolver is obtained by the getContentResolver() method of Context

Call its notifyChange() method to send a notification of data changes

2. In other programs, ContentObserver can be used to listen for data changes

ContentResolver is obtained by the getContentResolver() method of Context

Calling its registerContentObserver() method specifies that ContentObserver is registered with an Uri

Customize ContentObserver and override the onChange() method to get data

For example, listen for notifications when the user inserts data:


public Uri insert(Uri uri, ContentValues values) {
  SQLiteDatabase db = helper.getWritableDatabase();
  switch (matcher.match(uri)) {
    case PERSON_ALL:
      long id = db.insert("person", "id", values);
      //  Listen to inform 
      getContext().getContentResolver().notifyChange(uri, null);
      return ContentUris.withAppendedId(uri, id);
    default:
      throw new IllegalArgumentException("Unmatch Uri: " + uri);
  }
}

Listen in another project. Once the user performs insert operation, I can automatically get the record inserted by the user immediately:


public class MainActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getContentResolver().registerContentObserver(Uri.parse("content://cn.itcast.provider.itcast/person"), true, new MyObserver(new Handler()));
  }
  class MyObserver extends ContentObserver {
    public MyObserver(Handler handler) {
      super(handler);
    }
    // This method is called when the listener hears that the data has changed and queries the newly added data 
    public void onChange(boolean selfChange) {
      ContentResolver resolver = getContentResolver();
      Uri uri = Uri.parse("content://cn.itcast.provider.itcast/person");
      Cursor cursor = resolver.query(uri, new String[] { "id", "name", "phone", "balance" }, null, null, "id DESC LIMIT 1");
      while (cursor.moveToNext()) {
        System.out.print(cursor.getString(0) + " ");
        System.out.print(cursor.getString(1) + " ");
        System.out.print(cursor.getString(2) + " ");
        System.out.println(cursor.getString(3));
      }
    }
  }
}

Monitor and send SMS messages

1.Android system provides Provider to query SMS messages, and it will also send change notification when sending SMS messages

2. Define 1 Observer listening "content://sms"

3. Query the text message sent by the user "content://sms/outbox" in onChange() method

4. SMS send information stored in a database date/date/com android. providers. telephony

5. You need permission < uses-permission android:name="android.permission.READ_SMS" / >

Example:


public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(new Handler()));
}
// Listen for incoming text messages 
private final class SmsObserver extends ContentObserver {
  public SmsObserver(Handler handler) {
    super(handler);
  }
  public void onChange(boolean selfChange) {
    ContentResolver resolver = getContentResolver();
    // Check the text message sent 
    Uri uri = Uri.parse("content://sms/outbox");
    Cursor cursor = resolver.query(uri, new String[] { "date", "address", "body" }, null, null, "_id desc limit 1");
    if (cursor.moveToNext()) {
      long ms = cursor.getLong(0);
      String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(ms));
      String address = cursor.getString(1);
      String body = cursor.getString(2);
      System.out.println(date + " " + address + " " + body);
    }
  }
}

I hope this article has been helpful for Android programming.


Related articles: