android筆記-內容提供者

內容提供者(掌握)

  • 應用的數(shù)據(jù)庫是不允許其他應用訪問的

  • 內容提供者的作用就是讓別的應用訪問到你的私有數(shù)據(jù)

  • 自定義內容提供者,繼承ContentProvider類,重寫增刪改查方法,在方法中寫增刪改查數(shù)據(jù)庫的代碼,舉例增方法

      @Override
      public Uri insert(Uri uri, ContentValues values) {
          db.insert("person", null, values);
          return uri;
      }
    
  • 在清單文件中定義內容提供者的標簽,注意必須要有authorities屬性,這是內容提供者的主機名,功能類似地址

      <provider android:name="com.itheima.contentprovider.PersonProvider"
          android:authorities="com.itheima.person"
          android:exported="true"
       ></provider>
    
  • 創(chuàng)建一個其他應用,訪問自定義的內容提供者,實現(xiàn)對數(shù)據(jù)庫的插入操作

      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(掌握)

  • 用于判斷一條uri跟指定的多條uri中的哪條匹配

  • 添加匹配規(guī)則

      //指定多條uri
      um.addURI("com.itheima.person", "person", PERSON_CODE);
      um.addURI("com.itheima.person", "company", COMPANY_CODE);
      //#號可以代表任意數(shù)字
      um.addURI("com.itheima.person", "person/#", QUERY_ONE_PERSON_CODE);
    
  • 通過Uri匹配器可以實現(xiàn)操作不同的表

      @Override
      public 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;
      }
    
  • 如果路徑中帶有數(shù)字,把數(shù)字提取出來的api

      int id = (int) ContentUris.parseId(uri);
    

短信數(shù)據(jù)庫(掌握)

  • 只需要關注sms表
  • 只需要關注4個字段
    • body:短信內容
    • address:短信的發(fā)件人或收件人號碼(跟你聊天那哥們的號碼)
    • date:短信時間
    • type:1為收到,2為發(fā)送

讀取系統(tǒng)短信,首先查詢源碼獲得短信數(shù)據(jù)庫內容提供者的主機名和路徑,然后訪問內容提供者(掌握)

    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);
    }

插入系統(tǒng)短信(熟悉)

    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);
  • 插入查詢系統(tǒng)短信需要注冊權限

聯(lián)系人數(shù)據(jù)庫(掌握)

  • raw_contacts表:
    • contact_id:聯(lián)系人id
  • data表:聯(lián)系人的具體信息,一個信息占一行
    • data1:信息的具體內容
    • raw_contact_id:聯(lián)系人id,描述信息屬于哪個聯(lián)系人
    • mimetype_id:描述信息是屬于什么類型
  • mimetypes表:通過mimetype_id到該表查看具體類型

讀取聯(lián)系人(掌握)

  • 先查詢raw_contacts表拿到聯(lián)系人id

      Cursor cursor = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null);
    
  • 然后拿著聯(lián)系人id去data表查詢屬于該聯(lián)系人的信息

      Cursor c = cr.query(Uri.parse("content://com.android.contacts/data"), new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{contactId}, null);
    
  • 得到data1字段的值,就是聯(lián)系人的信息,通過mimetype判斷是什么類型的信息

      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);
          }
      }
    

插入聯(lián)系人(熟悉)

  • 先查詢raw_contacts表,確定新的聯(lián)系人的id應該是多少

  • 把確定的聯(lián)系人id插入raw_contacts表

      cv.put("contact_id", _id);
      cr.insert(Uri.parse("content://com.android.contacts/raw_contacts"), cv);
    
  • 在data表插入數(shù)據(jù)

    • 插3個字段: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);
      

內容觀察者(掌握)

  • 當數(shù)據(jù)庫數(shù)據(jù)改變時,內容提供者會發(fā)出通知,在內容提供者的uri上注冊一個內容觀察者,就可以收到數(shù)據(jù)改變的通知

      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
          }
    
          //內容觀察者收到數(shù)據(jù)庫發(fā)生改變的通知時,會調用此方法
          @Override
          public void onChange(boolean selfChange) {
    
          }
      
      }
    
  • 在內容提供者中發(fā)通知的代碼

      ContentResolver cr = getContext().getContentResolver();
      //發(fā)出通知,所有注冊在這個uri上的內容觀察者都可以收到通知
      cr.notifyChange(uri, null);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容