Android Basics Summary (10)

Source: Internet
Author: User

Content provider (Master)
    • The application's database is not allowed to be accessed by other apps
    • The role of content providers is to allow other applications to access your private data
    • Custom content provider, inherit the ContentProvider class, rewrite the method of adding and deleting, write and add or delete in the method to change the code of database, example increment method

      @Overridepublic Uri insert(Uri uri, ContentValues values) {    db.insert("person", null, values);    return uri;}
    • Define the label of the content provider in the manifest file, note that you must have the authorities attribute, which is the host name of the content provider, which functions like an address

      <provider android:name="com.itheima.contentprovider.PersonProvider"    android:authorities="com.itheima.person"    android:exported="true" ></provider>
    • Create a different app, access a custom content provider, implement an insert operation on the database

      public void click(View v){    //得到内容分解器对象    ContentResolver cr = getContentResolver();    ContentValues cv = new ContentValues();    cv.put("name", "小方");    cv.put("phone", 138856);    cv.put("money", 3000);    //url:内容提供者的主机名    cr.insert(Uri.parse("content://com.itheima.person"), cv);}
Urimatcher (Master)
    • Used to determine which match between a URI and a specified number of URIs
    • Add a matching rule

      //指定多条urium.addURI("com.itheima.person", "person", PERSON_CODE);um.addURI("com.itheima.person", "company", COMPANY_CODE);//#号可以代表任意数字um.addURI("com.itheima.person", "person/#", QUERY_ONE_PERSON_CODE);
    • Different tables can be implemented by URI-matching device

      @Overridepublic Uri insert(Uri uri, ContentValues values) {    if(um.match(uri) == PERSON_CODE){        db.insert("person", null, values);    }    else if(um.match(uri) == COMPANY_CODE){        db.insert("company", null, values);    }    else{        throw new IllegalArgumentException();    }    return uri;}
    • If the path has numbers, the API that pulls the numbers out

      int id = (int) ContentUris.parseId(uri);
SMS Database (Master)
    • Only need to focus on SMS tables
    • Just 4 fields to focus on
      • Body: SMS Content
      • Address: The sender or recipient number of the SMS (the buddy's number to chat with you)
      • Date: SMS Time
      • Type:1 for Receive, 2 for send
Read the system text message, first query the source to obtain the text message database content provider hostname and path, and then access the content provider (master)
    ContentResolver cr = getContentResolver();    Cursor c = cr.query(Uri.parse("content://sms"), new String[]{"body", "date", "address", "type"}, null, null, null);    while(c.moveToNext()){        String body = c.getString(0);        String date = c.getString(1);        String address = c.getString(2);        String type = c.getString(3);        System.out.println(body+";" + date + ";" + address + ";" + type);    }
Insert system SMS (familiar)
    ContentResolver cr = getContentResolver();    ContentValues cv = new ContentValues();    cv.put("body", "您尾号为XXXX的招行储蓄卡收到转账1,000,000人民币");    cv.put("address", 95555);    cv.put("type", 1);    cv.put("date", System.currentTimeMillis());    cr.insert(Uri.parse("content://sms"), cv);
    • Insert query system SMS requires registration permission
Contact Database (Master)
    • Raw_contacts table:
      • CONTACT_ID: Contact ID
    • Data table: Contact details, one line
      • DATA1: Specific content of information
      • RAW_CONTACT_ID: Contact ID that describes which contact the information belongs to.
      • MIMETYPE_ID: Describe what type of information it belongs to
    • Mimetypes table: View specific types by mimetype_id to this table
Read Contact (Master)
    • Check the Raw_contacts table first to get the contact ID.

      Cursor cursor = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null);
    • Then take the contact ID and go to the data table and query the information that belongs to the contact.

      Cursor c = cr.query(Uri.parse("content://com.android.contacts/data"), new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{contactId}, null);
    • Get the value of the Data1 field, that is, the contact information, through mimetype to determine what type of information

      while(c.moveToNext()){    String data1 = c.getString(0);    String mimetype = c.getString(1);    if("vnd.android.cursor.item/email_v2".equals(mimetype)){        contact.setEmail(data1);    }    else if("vnd.android.cursor.item/name".equals(mimetype)){        contact.setName(data1);    }    else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){        contact.setPhone(data1);    }}
Insert Contact (familiar)
    • Query the Raw_contacts table first to determine how much the new contact ID should be
    • Insert the identified contact ID into the raw_contacts table

      cv.put("contact_id", _id);cr.insert(Uri.parse("content://com.android.contacts/raw_contacts"), cv);
    • Inserting data in the database table

      • Insert 3 fields: Data1, MimeType, raw_contact_id

        cv = new ContentValues();cv.put("data1", "赵六");cv.put("mimetype", "vnd.android.cursor.item/name");cv.put("raw_contact_id", _id);cr.insert(Uri.parse("content://com.android.contacts/data"), cv);cv = new ContentValues();cv.put("data1", "1596874");cv.put("mimetype", "vnd.android.cursor.item/phone_v2");cv.put("raw_contact_id", _id);cr.insert(Uri.parse("content://com.android.contacts/data"), cv);
Content Viewer (Master)
    • When the database data changes, the content provider notifies you that a content watcher is registered on the content provider's URI to receive notification of changes to the data

      cr.registerContentObserver(Uri.parse("content://sms"), true, new MyObserver(new Handler()));class MyObserver extends ContentObserver{    public MyObserver(Handler handler) {        super(handler);        // TODO Auto-generated constructor stub    }    //内容观察者收到数据库发生改变的通知时,会调用此方法    @Override    public void onChange(boolean selfChange) {    }}
    • Code to send notifications to content providers

      ContentResolver cr = getContext().getContentResolver();//发出通知,所有注册在这个uri上的内容观察者都可以收到通知cr.notifyChange(uri, null);

Android Basics Summary (10)

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.