Content Viewer (a simple phone SMS bug)

Source: Internet
Author: User

I. Content Observer
* Notify content provider of changes
GetContext (). Getcontentresolver (). Notifychanges (Uri,null); Null indicates that there are no fixed receivers
* Write an observer in other applications and register an instance
Getcontentresolver (). Registercontentobserver (Uri,true,observer); The host data observed by the URI, true means that as long as the host matches, observer represents the specific observer
Example: SMS Bug

1. First write a myobserver inheritance contentobserver, overriding the OnChange method:public class myobserver extends  Contentobserver {    private context context;    public  myobserver (Context context, handler handler)  {         super (handler);        this.context = context;     }     @Override     public void  OnChange (Boolean selfchange, uri uri)  {         Super.onchange (Selfchange, uri);        //  fields in the SMS table read  : The  1 representative has read, 0 represents the field in the Unread         //  SMS table type :  2 represents the information that the monitored machine sends out, 1 represents the information received by the monitored device         //  gets the content parser          contentresOlver recolver = context.getcontentresolver ();         / /  query detection of the machine's system SMS         Cursor cursor =  Recolver.query (uri, new string[] {  "address",  "body",  "type",  "date"  } ,                null,  null,  "Date desc");         cursor.movetofirst ()  ;         //Get SMS Message         String  Address = cursor.getstring (0)  ;        string body  = cursor.getstring (1)  ;        int type =  cursor.getint (2)  ;        long date =  Cursor.getlong (3)  ; &NBSp;              if (TYPE == 2) {            string d = new  SimpleDateFormat ("yyyy mm month DD Day  HH:MM:SS"). Format (New date (Date))  ;             SYSTEM.OUT.PRINTLN ("The detected machine sent the message:  address:"  + address +   "  content:"  + body +  "Time  :"  + d );             toast.maketext (context,  "detected the machine sent the message:  address:"  +  address +  "  content:"  + body +  "Time  :"  + d, 0). Show ()  ;         }                 if (type == 1) {            &nBsp String d = new simpledateformat ("yyyy mm month DD Day  HH:MM:SS"). Format (New date (Date))  ;            SYSTEM.OUT.PRINTLN ("The detected machine receives information:   Address: " + address + "   content: " + body + " Time  : " + d  );             toast.maketext (context,  " The detected machine received the information:  address: " + address + "   content: " + body + " Time  : "  + d, 0). Show ()  ;        }           }}

2. write an observer in other applications and register an instance

Uri uri = uri.parse ("content://sms");//Monitored host
Getcontentresolver (). Registercontentobserver (URI, True, new Myobserver (This, new Handler ()));

This article is from the "Android Notes" blog, so be sure to keep this source http://2585211.blog.51cto.com/10044233/1665225

Content Viewer (a simple phone SMS bug)

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.