您的当前位置:首页正文

Android学习笔记(三)

来源:要发发知识网

◆Android数据库(SQLite)
▲创建数据库:
○boolean exits = getDatabasePath("stu.db").exists();//判断要创建的数据库是否存在。
○SQLiteDatabase db = context.openOrCreateDatabase(String dbName,int mode,CursorFactory factory);mode为访问模式,默认设为私有 MODE_PRIVATE

◆Cursor 主要方法:
△ boolean moveToNext():移动游标到下一个
△ boolean moveToPrevious():移动游标到上一个
△ boolean moveToFirst():移动游标到第一个
△ boolean moveToLast():移动游标到最后一个
△ boolean moveToPosition(int position): 移动游标到指定位置
△ boolean move(int offset):以当前位置为参考移动偏移量,如为正数向下移动n个,负数向上移动n个

◆取值:
△int getInt(int colIndex):
△String getString(int colIndex)

△int getColumnIndex(String columnName):通过字段取出索引
☆SQLite只支持通过列索引取列值,不支持通过列名取值,而Mysql还支持通过列名取值;故需将两个方法一起使用:c.getString(c.getColumnIndex("name")))

▲UriMatcher
构造方法:UriMathcer(int noMatch);new UriMatcher(UriMatcher.NO_MATCH)
▲主要方法:
§void addURI(String authority,String path,int code):path中*为通配;#为精确匹配。code为若成功匹配match(uri)后要返回的整数值。
§int match(Uri uri):匹配。

◆内容提供程序provider的Uri
scheme : content:// [android中定义的协议头]
authority : 一个自定义的字符串,唯一,授权字符串;
path :自定义的字符串,可包含请求的表
[id] :用于查、删、改的id,插入需要,可不指定。
◆ContentUris
static long parseId(Uri uri) :用于从一个单条数据中取出id值;
static Uri withAppendId(Uri uri,long id);用于将id和一个uri拼接成一个单条数据;

●使用ContentProvider提供数据的步骤:
→1、创建数据库
→2、创建一个类继承自ContentProvider
→3、注册:提供授权字符串(authority);在Manifest文件中添加

<provider
            android:name=".Provider"  //类名
            android:authorities="com.fanfan.provider"   //授权字符串
            android:exported="true" >  //为true为才对外公开,为false不对外公开
</provider>

◆媒体库:
△相关的表和视图
audio : _id _data _display_name _size title album album_key artist composer duration
album_info : _id album album_key artist album_art
artist_info: _id artist artist_key
音频:
◆播放:

MediaPlayer player = new MediaPlayer();
player.reset();//先设置再装入数据;
player.setDataSource(m.getMusicPath());//包括其他参数重写方法
player.prepare();//需要先准备预读,再开始播放。
player.start();
player.release();//在Service或要退出程序时应用此方法释放播放器
player.pause();