Android uses contentobserver to listen for database changes

Source: Internet
Author: User

Recently, a friend asked me how to receive text messages from a specified number, and asked me not to allow the system to intercept messages to users. It's a good time to look at tianchao group. A friend also asked this question and solved it through the contentobserver method. I will paste the code I implemented here for your reference.

You can listen to the contentprovider of the corresponding Uri, and implement what you want to do or broadcast in onchange.

[Java]
View plaincopy
  1. Public class screentest extends activity {
  2. Class smscontent extends contentobserver {
  3. Private cursor = NULL;
  4. Public smscontent (handler ){
  5. Super (handler );
  6. }
  7. /**
  8. * @ Description this method is called when the text message table is changed
  9. * Two permissions are required.
  10. * Android. Permission. read_sms reads text messages
  11. * SMS writing for Android. Permission. write_sms
  12. * @ Author snake
  13. * @ Date 2010-1-12
  14. */
  15. @ Override
  16. Public void onchange (Boolean selfchange ){
  17. // Todo auto-generated method stub
  18. Super. onchange (selfchange );
  19. // Read the text message of the specified number in the inbox
  20. Cursor = managedquery (URI. parse ("content: // SMS/inbox"), new string [] {"_ id", "Address", "read"}, "address =? And read =? ", New string [] {" 12345678901 "," 0 "}," date DESC ");
  21. If (cursor! = NULL ){
  22. Contentvalues values = new contentvalues ();
  23. Values. Put ("read", "1"); // modify the text message to read mode.
  24. Cursor. movetofirst ();
  25. While (cursor. islast ()){
  26. // Update the status of the currently unread SMS to read
  27. Getcontentresolver (). Update (URI. parse ("content: // SMS/inbox"), values, "_ id =? ", New string [] {" "+ cursor. getint (0 )});
  28. Cursor. movetonext ();
  29. }
  30. }
  31. }
  32. }
  33. /** Called when the activity is first created .*/
  34. @ Override
  35. Public void oncreate (bundle savedinstancestate ){
  36. Super. oncreate (savedinstancestate );
  37. Setcontentview (R. layout. Main );
  38. Smscontent content = new smscontent (new handler ());
  39. // Register the SMS change listener
  40. This. getcontentresolver (). registercontentobserver (URI. parse ("content: // SMS/"), true, content );
  41. }
  42. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.