Android复习08【内容提供者】

   日期:2020-05-29     浏览:126    评论:0    
核心提示:ContentProvider初探思维导图资源网站添加手机联系人Android使用MediaStore获取手机上的文件日历操作手机联系人移动开发

2020-04-14-第9周-周二 

目   录

思维导图

资源网站

添加手机联系人

Android使用MediaStore获取手机上的文件

日历操作

手机联系人

MainActivity.java

MyHelper.java 

PersonCp.java

思维导图

 

资源网站

菜鸟教程---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 {

    @Override
    protected 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;


public class MyHelper extends SQLiteOpenHelper {

    public MyHelper(@Nullable Context context) {
        super(context, "person.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table person(id integer primary key autoincrement," +
                "name varchar(20),phone varchar(12),salary Integer(12))");
    }

    @Override
    public 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;


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;

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


    
    @Override
    public 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());
        }
    }


    @Override
    public boolean onCreate() {
        mHelper = new MyHelper(getContext());
        return true;
    }


    
    @Override
    public 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());
        }
    }

    
    @Override
    public 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;
    }

    
    @Override
    public 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;
    }

    
    @Override
    public 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;
    }
}

 

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
更多>相关资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服