Android复习08【内容提供者】
生活随笔
收集整理的這篇文章主要介紹了
Android复习08【内容提供者】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2020-04-14-第9周-周二?
目? ?錄
思維導圖
資源網站
添加手機聯系人
Android使用MediaStore獲取手機上的文件
日歷操作
手機聯系人
MainActivity.java
MyHelper.java?
PersonCp.java
思維導圖
https://share.weiyun.com/1vVLYnlb
?
資源網站
菜鳥教程---4.4.1 ContentProvider初探
https://www.runoob.com/w3cnote/android-tutorial-contentprovider.html
?
https://www.cnblogs.com/sparrowlhl/p/11239530.html
?
https://developer.android.google.cn/guide/topics/providers/content-provider-creating
?
https://www.jb51.net/article/122840.htm
?
添加手機聯系人
靜態代碼塊---自動執行一次。
Android使用MediaStore獲取手機上的文件
https://blog.csdn.net/yann02/article/details/92844364??
日歷操作
手機聯系人
MainActivity.java
package cn.wangzg.personcp;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);} }MyHelper.java?
package cn.wangzg.personcp;import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;/*** Time: 2020/4/13* Author: wangzhiguo* Description: 功能描述*/ public class MyHelper extends SQLiteOpenHelper {public MyHelper(@Nullable Context context) {super(context, "person.db", null, 1);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table person(id integer primary key autoincrement," +"name varchar(20),phone varchar(12),salary Integer(12))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} }PersonCp.java
package cn.wangzg.personcp;import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri;import java.util.Objects;/*** Time: 2020/4/13* Author: wangzhiguo* Description: 功能描述*/ public class PersonCp extends ContentProvider { //數據庫作為數據源,將數據保存到數據庫中。private MyHelper mHelper;private final static String AUTHORITY = "cn.wangzg.personprovider";private static UriMatcher mUriMatcher;private static final int PERSON_DIR = 0;private static final int PERSON = 1;/*** 利用靜態代碼塊初始化UriMatcher* 在UriMatcher中包含了多個Uri,每個Uri代表一種操作* 當調用UriMatcher.match(Uri uri)方法時就會返回該uri對應的code;* 比如此處的PERSONS和PERSON*/static {mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);// 該URI表示返回所有的person,其中PERSONS為該特定Uri的標識碼mUriMatcher.addURI(AUTHORITY, "person", PERSON_DIR);// 該URI表示返回某一個person,其中PERSON為該特定Uri的標識碼mUriMatcher.addURI(AUTHORITY, "person/#", PERSON);}/*** 在自定義ContentProvider中必須覆寫getType(Uri uri)方法.* 該方法用于獲取Uri對象所對應的MIME類型.* <p>* 一個Uri對應的MIME字符串遵守以下三點:* 1 必須以vnd開頭* 2 如果該Uri對應的數據可能包含多條記錄,那么返回字符串應該以"vnd.android.cursor.dir/"開頭* 3 如果該Uri對應的數據只包含一條記錄,那么返回字符串應該以"vnd.android.cursor.item/"開頭*/@Overridepublic String getType(Uri uri) {switch (mUriMatcher.match(uri)) {case PERSON_DIR:return "vnd.android.cursor.dir/" + AUTHORITY + ".persons";case PERSON:return "vnd.android.cursor.item/" + AUTHORITY + ".person";default:throw new IllegalArgumentException("unknown uri" + uri.toString());}}@Overridepublic boolean onCreate() {mHelper = new MyHelper(getContext());return true;}/*** 插入操作:* 插入操作只有一種可能:向一張表中插入* 返回結果為新增記錄對應的Uri* 方法db.insert()返回結果為新增記錄對應的主鍵值*/@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = mHelper.getWritableDatabase();switch (mUriMatcher.match(uri)) {case PERSON_DIR:long newId = db.insert("person", "name,phone,salary", values);//向外界通知該ContentProvider里的數據發生了變化 ,以便ContentObserver作出相應getContext().getContentResolver().notifyChange(uri, null);return ContentUris.withAppendedId(uri, newId);default:throw new IllegalArgumentException("unknown uri" + uri.toString());}}/*** 更新操作:* 更新操作有兩種可能:更新一張表或者更新某條數據* 在更新某條數據時原理類似于查詢某條數據,見下.*/@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {SQLiteDatabase db = mHelper.getWritableDatabase();int updatedNum = 0;switch (mUriMatcher.match(uri)) {// 更新表case PERSON_DIR:updatedNum = db.update("person", values, selection, selectionArgs);break;// 按照id更新某條數據case PERSON:long id = ContentUris.parseId(uri);String where = "id=" + id;if (selection != null && !"".equals(selection.trim())) {where = selection + " and " + where;}updatedNum = db.update("person", values, where, selectionArgs);break;default:throw new IllegalArgumentException("unknown uri" + uri.toString());}//向外界通知該ContentProvider里的數據發生了變化 ,以便ContentObserver作出相應Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);return updatedNum;}/*** 刪除操作:* 刪除操作有兩種可能:刪除一張表或者刪除某條數據* 在刪除某條數據時原理類似于查詢某條數據,見下.*/@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = mHelper.getWritableDatabase();int deletedNum = 0;switch (mUriMatcher.match(uri)) {// 刪除表case PERSON_DIR:deletedNum = db.delete("person", selection, selectionArgs);break;// 按照id刪除某條數據case PERSON:long id = ContentUris.parseId(uri);String where = "id=" + id;if (selection != null && !"".equals(selection.trim())) {where = selection + " and " + where;}deletedNum = db.delete("person", where, selectionArgs);break;default:throw new IllegalArgumentException("unknown uri" + uri.toString());}//向外界通知該ContentProvider里的數據發生了變化 ,以便ContentObserver作出相應Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);return deletedNum;}/*** 查詢操作:* 查詢操作有兩種可能:查詢一張表或者查詢某條數據* <p>* 注意事項:* 在查詢某條數據時要注意--因為此處是按照id來查詢* 某條數據,但是同時可能還有其他限制.例如:* 要求id為2且name為xiaoming1* 所以在查詢時分為兩步:* 第一步:* 解析出id放入where查詢條件* 第二步:* 判斷是否有其他限制(如name),若有則將其組拼到where查詢條件.* <p>* 詳細代碼見下.*/@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {SQLiteDatabase db = mHelper.getWritableDatabase();Cursor cursor = null;switch (mUriMatcher.match(uri)) {// 查詢表case PERSON_DIR:cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);break;// 按照id查詢某條數據case PERSON:// 第一步:long id = ContentUris.parseId(uri);String where = "id=" + id;// 第二步:if (selection != null && !"".equals(selection.trim())) {where = selection + " and " + where;}cursor = db.query("person", projection, where, selectionArgs, null, null, sortOrder);break;default:throw new IllegalArgumentException("unknown uri" + uri.toString());}return cursor;} }蟹蟹觀看~~~
總結
以上是生活随笔為你收集整理的Android复习08【内容提供者】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue.js-localhost:808
- 下一篇: Vue.js-Day03-PM【组件通信