android音樂播放器文章,Android復習09【內容提供者、音樂播放器】

目 錄

PersonCp

PersonCp.java

insert()

ContentObserver

音樂播放器

1、添加讀寫權限

1.1、動態權限授予(調用封裝好的方法)

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

2、Music.java(實體類)

申請訪問SD卡的權限

設置適配器

下拉刷新

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;

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;

}

}

insert()

af7c1aeb64b99e38aca3ca523a505146.png

ContentObserver

菜鳥教程 【4.4.1 ContentProvider初探】

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

0788806f5fdd1e4e43f9a754b6ebb6bb.png

音樂播放器

1、添加讀寫權限

5ec4ae5075b76a1f8ed52a62cb812398.png

1.1、動態權限授予(調用封裝好的方法)

e8728367b418156cb34309c0c0ee79d7.png

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

eb440abb3a01a9d7422167a46c9353ec.png

2、Music.java(實體類)

申請訪問SD卡的權限

43b68b0749985151d6f23fbb14ea42b5.png

設置適配器

25bec743762a7b6fa811ea9e9d51413d.png

下拉刷新

82563e59b7ca9a75e7bb5790893591db.png

點個贊吧~? ? ? b( ̄▽ ̄)d

求求了~

小本生意,不容易啊~

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458845.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458845.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458845.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

程序員的業余項目

程序員的業余項目&#xff0c;我們也叫它 side project。 前幾天&#xff0c;100offer 發起了一場活動叫 <尋找實干和堅持的技術力量>&#xff0c;他們是這么說的&#xff1a; 世界在被代碼改變著&#xff0c;而我們在創造著代碼。 僅僅是因為好玩&#xff0c;他開發了…

C語言的數組名和對數組名取地址

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 相信不少的C語言初學者都知道&#xff0c;數組名相當于指針&#xff0c;指向數組的首地…

小米 android 8,小米華為們誰最良心?10大手機廠商安卓8.0升級情況盤點

3月8日&#xff0c;谷歌放出了首個安卓9.0開發者預覽版的固件包&#xff0c;不出意外的話&#xff0c;它的正式版會在今年正式亮相。但對廣大安卓用戶來說&#xff0c;想要立刻用上最新系統并非易事。目前來說&#xff0c;安卓碎片化問題依然嚴重&#xff0c;我們不妨現實點&am…

窺探Swift之數組安全索引與數組切片

在Swift中的數組和字典中下標是非常常見的&#xff0c;數組可以通過索引下標進行元素的查詢&#xff0c;字典可以通過鍵下標來獲取相應的值。在使用數組時&#xff0c;一個常見的致命錯誤就是數組越界。如果在你的應用程序中數組越界了&#xff0c;那么對不起&#xff0c;如果由…

大小端模式的快速判斷方法

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 大小端的問題剖析&#xff1a; 嵌 入式系統開發者應該對Little-endian和Big-endian模…

【RAC】How to Proceed from Failed 11gR2 CRS Installation

Applies to: [ID 942166.1] Oracle Server – Enterprise Edition – Version: 11.2.0.1 to 11.2.0.2 – Release: 11.2 to 11.2 Generic UNIX Generic Linux Goal This goal of this note is to provide steps to proceed from failed 11gR2 Grid Infrastructure installat…

WinForm支持拖拽效果

有一個MSDN客戶提問在WinForm中如何實現拖拽效果——比如在WinForm中有一個Button&#xff0c;我要實現的效果是拖拽這個Button到目標位置后生成一個該控件的副本。 其實這個操作主要分成三步走&#xff1a; 1&#xff09;確定被拖拽的對象&#xff1a; 這里是Button&#xff0…

win7 64位出現桌面右鍵鼠標顯示忙碌

*************************************************** 更多精彩&#xff0c;歡迎進入&#xff1a;http://shop115376623.taobao.com *************************************************** 將下面綠色內容復制到txt文本中&#xff0c;然后另存為1.bat 雙擊運行即可 【針對64位…

android tee,Android 9.0的新增安全特性與TEE

Android P&#xff0c;預計將于 2018 年第三季度發布最終版本。特別是Android8.0以來&#xff0c;安全性是Android版本變更的一個重要因素。從安全性增強方面來看&#xff0c;本次Android9.0版本主要有以下幾個方面&#xff1a;統一的指紋身份驗證對話框Android P 中&#xff0…

哪些要素會讓咱們呈現抑郁癥的病癥

依據最新研討標明&#xff0c;一自個的性情怎樣&#xff0c;本來是天然生成的&#xff0c;后天的日子&#xff0c;僅僅對咱們的性情進行批改&#xff0c;但在咱們潛意識中&#xff0c;違反自個性情的行動&#xff0c;會讓咱們感到格外累&#xff0c;所以&#xff0c;不少人即是…

如何定義一個只能在堆上(棧上)生成對象的類?

在C中&#xff0c;類的對象建立分為兩種&#xff0c;一種是靜態建立&#xff0c;如A a&#xff1b;另一種是動態建立&#xff0c;如A* ptrnew A&#xff1b;這兩種方式是有區別的。 靜態建立一個類對象&#xff0c;是由編譯器為對象在棧空間中分配內存&#xff0c;是通過直接移…

canny算子的理論分析

****************************************************************************************************************************************** 紅&#xff1a;數字圖像處理視頻教程&#xff08;兩部&#xff09; {中科院版36講視頻教程 電子科大版70講視頻教程&#x…

Android為spinner設置適配器,Android Spinner與適配器模式詳解及實例代碼

最近做項目對Android Spinner 使用&#xff0c;這里簡單寫個小例子&#xff0c;來測試如何使用。Spinner是一個下拉列表&#xff0c;往安卓界面中拖拽一個Spinner控件&#xff0c;在屬性中設置Android:entries“array/spinner_data”其中spinner_data為在string中設置的數組。數…

web框架-Struts開始

問題&#xff1a; 為什么有structs 作為一種框架&#xff08;frameset&#xff09;可以與傳統的mvc進行比較&#xff1f; MVC是一種模式數據處理、顯示和數據輸入分開&#xff0c;來規范開發&#xff0c;但是卻又并不規范。可以這樣想&#xff1a;有三家公司&#xff0c;他們對…

加快上架方法

估計最近蘋果app應用上架的比較多&#xff0c;審核比較慢&#xff0c;現在一個app從提交到上架短則7&#xff0c;8天&#xff0c;長則2&#xff0c;3個星期。我在實際上線應用時&#xff0c;總結了一個簡單實用的小技巧&#xff0c;可以加快上架時間&#xff0c;最近使用這種方…

接口自動化測試 返回html,接口自動化測試實戰(更新完畢)

前言自動化沒練習的項目怎么辦&#xff1f;自動化已經成為測試的必備技能之一了&#xff0c;所以&#xff0c;很多想跳槽的測試朋友都在自學&#xff0c;特別是最實用的接口自動化&#xff0c;但是很多人因為沒有可以練手的項目而苦惱&#xff0c;最終導致缺乏實戰經驗&#xf…

Opencv 圖像增強算法 圖像檢測結果及代碼

****************************************************************************************************************************************** 紅&#xff1a;數字圖像處理視頻教程&#xff08;兩部&#xff09; {中科院版36講視頻教程 電子科大版70講視頻教程&#x…

php Hash Table(四) Hash Table添加和更新元素

HashTable添加和更新的函數&#xff1a; 有4個主要的函數用于插入和更新HashTable的數據: int zend_hash_add(HashTable *ht, char *arKey, uint nKeyLen,void **pData, uint nDataSize, void *pDest); int zend_hash_update(HashTable *ht, char *arKey, uint nKeyLen, void …

山寨“餓了么”應用中添加菜品數量按鈕效果

山寨“餓了么”應用中添加菜品數量按鈕效果 本人視頻教程系類 iOS中CALayer的使用 最終效果&#xff1a; 山寨源頭&#xff1a; 源碼&#xff1a;&#xff08;此源碼解決了重用問題&#xff0c;可以放心的放在cell中使用&#xff09; AddAndDeleteButton.h 與 AddAndDeleteBu…

html間數據傳送,Express框架與html之間如何進行數據傳遞(示例代碼)

關于Node.js 的Express框架介紹&#xff0c;推薦看菜鳥教程的Express框架&#xff0c;很適合入門&#xff0c;這里不再贅述&#xff0c;這里主要講一下Express框架與html之間如何進行數據傳遞我采用的是JQuery的Ajax()向后臺傳參方式(url傳參)1、Type屬性為Get時&#xff1a;(1…