日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Android复习09【内容提供者、音乐播放器(附完整工程文件)】

發(fā)布時(shí)間:2024/9/30 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android复习09【内容提供者、音乐播放器(附完整工程文件)】 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2020-04-16【第9周---周四】

音樂(lè)播放器Android代碼下載:https://wws.lanzous.com/ifqzihaxvij

目 錄

PersonCp

PersonCp.java

insert()

ContentObserver

音樂(lè)播放器

1、添加讀寫(xiě)權(quán)限

1.1、動(dòng)態(tài)權(quán)限授予(調(diào)用封裝好的方法)

2、獲取音樂(lè)文件(MainActivity.java)

2、Music.java(實(shí)體類(lèi))

申請(qǐng)?jiān)L問(wèn)SD卡的權(quán)限

設(shè)置適配器

下拉刷新


PersonCp

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 { //數(shù)據(jù)庫(kù)作為數(shù)據(jù)源,將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)中。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;/*** 利用靜態(tài)代碼塊初始化UriMatcher* 在UriMatcher中包含了多個(gè)Uri,每個(gè)Uri代表一種操作* 當(dāng)調(diào)用UriMatcher.match(Uri uri)方法時(shí)就會(huì)返回該uri對(duì)應(yīng)的code;* 比如此處的PERSONS和PERSON*/static {mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);// 該URI表示返回所有的person,其中PERSONS為該特定Uri的標(biāo)識(shí)碼mUriMatcher.addURI(AUTHORITY, "person", PERSON_DIR);// 該URI表示返回某一個(gè)person,其中PERSON為該特定Uri的標(biāo)識(shí)碼mUriMatcher.addURI(AUTHORITY, "person/#", PERSON);}/*** 在自定義ContentProvider中必須覆寫(xiě)getType(Uri uri)方法.* 該方法用于獲取Uri對(duì)象所對(duì)應(yīng)的MIME類(lèi)型.* <p>* 一個(gè)Uri對(duì)應(yīng)的MIME字符串遵守以下三點(diǎn):* 1 必須以vnd開(kāi)頭* 2 如果該Uri對(duì)應(yīng)的數(shù)據(jù)可能包含多條記錄,那么返回字符串應(yīng)該以"vnd.android.cursor.dir/"開(kāi)頭* 3 如果該Uri對(duì)應(yīng)的數(shù)據(jù)只包含一條記錄,那么返回字符串應(yīng)該以"vnd.android.cursor.item/"開(kāi)頭*/@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;}/*** 插入操作:* 插入操作只有一種可能:向一張表中插入* 返回結(jié)果為新增記錄對(duì)應(yīng)的Uri* 方法db.insert()返回結(jié)果為新增記錄對(duì)應(yīng)的主鍵值*/@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里的數(shù)據(jù)發(fā)生了變化 ,以便ContentObserver作出相應(yīng)getContext().getContentResolver().notifyChange(uri, null);return ContentUris.withAppendedId(uri, newId);default:throw new IllegalArgumentException("unknown uri" + uri.toString());}}/*** 更新操作:* 更新操作有兩種可能:更新一張表或者更新某條數(shù)據(jù)* 在更新某條數(shù)據(jù)時(shí)原理類(lèi)似于查詢(xún)某條數(shù)據(jù),見(jiàn)下.*/@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更新某條數(shù)據(jù)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里的數(shù)據(jù)發(fā)生了變化 ,以便ContentObserver作出相應(yīng)Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);return updatedNum;}/*** 刪除操作:* 刪除操作有兩種可能:刪除一張表或者刪除某條數(shù)據(jù)* 在刪除某條數(shù)據(jù)時(shí)原理類(lèi)似于查詢(xún)某條數(shù)據(jù),見(jiàn)下.*/@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刪除某條數(shù)據(jù)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里的數(shù)據(jù)發(fā)生了變化 ,以便ContentObserver作出相應(yīng)Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);return deletedNum;}/*** 查詢(xún)操作:* 查詢(xún)操作有兩種可能:查詢(xún)一張表或者查詢(xún)某條數(shù)據(jù)* <p>* 注意事項(xiàng):* 在查詢(xún)某條數(shù)據(jù)時(shí)要注意--因?yàn)榇颂幨前凑読d來(lái)查詢(xún)* 某條數(shù)據(jù),但是同時(shí)可能還有其他限制.例如:* 要求id為2且name為xiaoming1* 所以在查詢(xún)時(shí)分為兩步:* 第一步:* 解析出id放入where查詢(xún)條件* 第二步:* 判斷是否有其他限制(如name),若有則將其組拼到where查詢(xún)條件.* <p>* 詳細(xì)代碼見(jiàn)下.*/@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {SQLiteDatabase db = mHelper.getWritableDatabase();Cursor cursor = null;switch (mUriMatcher.match(uri)) {// 查詢(xún)表case PERSON_DIR:cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);break;// 按照id查詢(xún)某條數(shù)據(jù)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;} }

insert()

ContentObserver

菜鳥(niǎo)教程 【4.4.1 ContentProvider初探】?

https://www.runoob.com/w3cnote/android-tutorial-contentprovider.html

音樂(lè)播放器

1、添加讀寫(xiě)權(quán)限

1.1、動(dòng)態(tài)權(quán)限授予(調(diào)用封裝好的方法)

2、獲取音樂(lè)文件(MainActivity.java)

2、Music.java(實(shí)體類(lèi))

申請(qǐng)?jiān)L問(wèn)SD卡的權(quán)限

設(shè)置適配器

下拉刷新

點(diǎn)個(gè)贊吧~? ? ? b( ̄▽ ̄)d 👍

求求了~

小本生意,不容易啊~

總結(jié)

以上是生活随笔為你收集整理的Android复习09【内容提供者、音乐播放器(附完整工程文件)】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。