android ContentProvider内容提供者

时间:2022-01-07 09:20:07

  AndroidManifest  --------

<!-- 配置内容提供者 -->
<provider
android:name=".provider.MyContentProvider"
android:authorities="com.zking.phon.PERSON"
android:exported="true" />

 再写一个类   ---继承ContentProvider
 public class MyContentProvider extends ContentProvider
   再重写几个方法  ----
 public boolean onCreate() ;
 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);
 public String getType(Uri uri);
 public Uri insert(Uri uri, ContentValues values);
 public int delete(Uri uri, String selection, String[] selectionArgs);
 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs);
   再其他的app调用  ContentResolver
 //获取内容访问者 cr = getContentResolver(); uri = Uri.parse("content://com.zking.phon.PERSON/student");    Cursor cursor=cr.query(uri,null,null,null,null);    Log.i("ccc"," sss" +cursor);    Toast.makeText(this, "hhh"+cursor, Toast.LENGTH_SHORT).show(); while (cursor.moveToNext()){      int id=cursor.getInt(cursor.getColumnIndex("_id"));      String name=cursor.getString(cursor.getColumnIndex("name"));      int age=cursor.getInt(cursor.getColumnIndex("age"));       Log.i("ccc",id+" "+name+" "+age);     }