android基礎之內容提供者創建

一、什麼是ContentProvider(內容提供者)呢?ContentProvider屬於安卓四大組件之一。ContentProvider將訪問的方式統一,如數據庫、文件等。都可以通過ContentProvider來訪問,不必針對不同的數據類型採取不同的訪問方式。這樣我們就可以控制那些內容,其他程序可以訪問,那些內容其他程序不可以訪問,即限制了其他程序的訪問權限,保證我們的數據安全性。ContentProvider的數據更改可被監聽,如:監聽信息的變化,通話記錄的變化等。我們就可以採取相應的一些操作。

二、怎麼創建ContentProvider?分爲2步。

1、創建一個類繼承ContentProvider(抽象類),重寫裏面的六個抽象方法

2、在清單文件(AndroidManifest.xml)中註冊,在application節點下創建一個provider節點。必須要指定name,authorities屬性

name:我們創建類的名稱,要寫全類名

authorities:一個唯一標示,標示我們這個內容提供者只有一個,一般情況下我們就用域名錶示,如:com.51cto.blog.6272409

  1. static{ 
  2.         UriMatcher.addURI("com.51cto.providers.personprovider", "person", 1); 
  3.         //#在java中代表數字 
  4.         UriMatcher.addURI("com.51cto.providers.personprovider", "person/#", 2); 
  5.          
  6.     } 
  1. @Override 
  2.     public boolean onCreate() { //當PersonProvider實例被創建之後,系統調用,只調用一次 
  3.         //數據庫的初始化操作,一般情況下把數據庫的初始放在這個方法裏面
  4.         SQLiteOpenHelper msoh = new MySQLiteOpenHelper(this.getContext()); 
  5.         System.out.println("onCreate執行了"); 
  6.         return true
  7.     } 
  8.     @Override 
  9.     public Cursor query(Uri uri, String[] projection, String selection, 
  10.             String[] selectionArgs, String sortOrder) {//可以供外部的應用,查詢內容提供者的數據 
  11.         SQLiteDatabase db = msoh.getReadableDatabase(); 
  12.         //判斷Uri 通過一個類 UriMatcher 
  13.         switch(MATCHER.match(uri)){ 
  14.             case 1
  15.                 return db.query("person", projection, selection, selectionArgs, nullnull, sortOrder); 
  16.             case 2
  17.                 //通過ContentUris得到id,可以截取uri中的id
  18.                 long id = ContentUris.parseId(uri); 
  19.                 String where = "id="+id; 
  20.                 //判斷外面是否有條件傳進來 
  21.                 if(selection!=null && !"".equals(selection.trim())){ 
  22.                        where += " and "+selection; 
  23.                 } 
  24.                 return db.query("person", projection, where, selectionArgs, nullnull , sortOrder); 
  25.             default
  26.                 throw new IllegalArgumentException("不是這個"+uri); 
  27.         } 
  28.     } 
  29.  
  30.     @Override 
  31.     public String getType(Uri uri) {//返回要操作的內容類型
  32.         switch(MATCHER.match(uri)){ 
  33.         case 1
  34.             return "vnd.android.cursor.dir/person";//多條數據
  35.         case 2
  36.             return "vnd.android.cursor.item/person";//單條數據
  37.              
  38.         default
  39.             throw new IllegalArgumentException("不是這個"+uri); 
  40.      
  41.         } 
  42.     } 
  43.  
  44.     @Override 
  45.     public Uri insert(Uri uri, ContentValues values) { 
  46.         //得到數據庫 
  47.         SQLiteDatabase db = msoh.getWritableDatabase(); 
  48.         //判斷Uri 通過一個類 UriMatcher 
  49.         switch(MATCHER.match(uri)){ 
  50.             case 1
  51.                 long rowId = db.insert("person""id", values);//主鍵值  
  52.                 //工具類   ContentUris.withAppendedId(contentUri, id)將以個id添加到uri中
  53.                 Uri insertUri = ContentUris.withAppendedId(uri, rowId); 
  54.                 this.getContext().getContentResolver().notifyChange(uri, null); 
  55.                 return insertUri; 
  56.                  
  57.             default
  58.                 throw new IllegalArgumentException("不是這個"+uri); 
  59.          
  60.         } 
  61.     } 
  62.  
  63.     @Override 
  64.     public int delete(Uri uri, String selection, String[] selectionArgs) {//放回影響到的記錄數 
  65.         //得到數據庫 
  66.         SQLiteDatabase db = msoh.getWritableDatabase(); 
  67.         int count = 0
  68.         //判斷Uri 通過一個類 UriMatcher 
  69.         switch(MATCHER.match(uri)){ 
  70.             case 1
  71.                  count = db.delete("person", selection, selectionArgs); 
  72.                     this.getContext().getContentResolver().notifyChange(uri, null); 
  73.                 //工具類   ContentUris.withAppendedId(contentUri, id) 
  74.                  break
  75.             case 2
  76.                 //通過ContentUris得到id  
  77.                 long id = ContentUris.parseId(uri); 
  78.                 String where = "id="+id; 
  79.                 //判斷外面是否有條件傳進來 
  80.                 if(selection!=null && !"".equals(selection.trim())){ 
  81.                        where += " and "+selection; 
  82.                 } 
  83.                 count = db.delete("person", where, selectionArgs); 
  84.                 break
  85.             default
  86.                 throw new IllegalArgumentException("不是這個"+uri); 
  87.         } 
  88.         return count; 
  89.     } 
  90.  
  91.     @Override 
  92.     public int update(Uri uri, ContentValues values, String selection, 
  93.             String[] selectionArgs) { 
  94.         SQLiteDatabase db = msoh.getWritableDatabase(); 
  95.         int count = 0
  96.         //判斷Uri 通過一個類 UriMatcher,之前可以調用UriMatcher.addURI();添加一個
  97.         switch(MATCHER.match(uri)){ 
  98.             case 1
  99.                  count = db.update("person", values, selection, selectionArgs); 
  100.                 //工具類   ContentUris.withAppendedId(contentUri, id) 
  101.                  break
  102.             case 2
  103.                 //通過ContentUris得到id  
  104.                 long id = ContentUris.parseId(uri); 
  105.                 String where = "id="+id; 
  106.                 //判斷外面是否有條件傳進來 
  107.                 if(selection!=null && !"".equals(selection.trim())){ 
  108.                        where += " and "+selection; 
  109.                 } 
  110.                 count = db.update("person", values, where, selectionArgs); 
  111.                 break;       
  112.             default
  113.                 throw new IllegalArgumentException("不是這個"+uri); 
  114.         } 
  115.         return count; 
  116.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章