内容提供者
首先创建数据库
public class DbOpenHelper extends SQLiteOpenHelper {
public DbOpenHelper(Context context) {
super(context, DbConst.DB_NAME, null
, DbConst.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+DbConst.CONTACT_TABLE+"("+ DbConst._ID+" integer primary key autoincrement,"+DbConst._USERNAME+" text ,"+DbConst._PHONE+" text);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MyContentProvider extends ContentProvider {
private DbOpenHelper mHelper;
@Override
public boolean onCreate() {
mHelper = new DbOpenHelper(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mHelper.getReadableDatabase();
return db.query(DbConst.CONTACT_TABLE,
projection,
selection,
selectionArgs,
null,
null,
null);
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = mHelper.getReadableDatabase();
long id = db.insert(DbConst.CONTACT_TABLE, null, values);
String str = uri.toString();
str=str+"/"+id;
return Uri.parse(str);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
}
内容解析者
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void query(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.example.ReviewForDatabase/contact/1");
String type = cr.getType(uri);
Log.v("meeeeeee", ""+type);
Cursor cursor = cr.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String username = cursor.getString(1);
String phone = cursor.getString(2);
Log.v("meeeeeee", ""+username +" "+phone);
}
cursor.close();
}
public void insert(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.example.ReviewForDatabase");
ContentValues value = new ContentValues();
createData(value, "你妹","啊");
Uri newuri = cr.insert(uri, value);
Log.v("meeeeeee", " "+(newuri==null));
}
private void createData(ContentValues value,String str1,String str2) {
value.put("phone", str2);
value.put("username", str1);
}
public void delete(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.example.ReviewForDatabase/contact/1");
int x = cr.delete(uri, null, null);
Log.v("meeeeeee", "你猜删除了多少行...? \n答案是"+x);
}
public void modify(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.example.ReviewForDatabase/contact/1");
ContentValues value = new ContentValues();
value.put("username", "移动营业厅");
value.put("phone", "10086");
int raws = cr.update(uri, value, null, null);
Log.v("meeeeeee", "修改了"+raws+"行");
}
}
内容匹配者
public class MyContentProvider extends ContentProvider {
private static final String AUTHORITY="com.example.ReviewForDatabase";
private static UriMatcher sUriMatcher = new UriMatcher(0);
static {
sUriMatcher.addURI(AUTHORITY,"#",1);
sUriMatcher.addURI(AUTHORITY,"contact",2);
sUriMatcher.addURI(AUTHORITY,"contact/#",3);
}
private DbOpenHelper mHelper;
@Override
public boolean onCreate() {
mHelper = new DbOpenHelper(getContext());
return false;
}
@Override
public String getType(Uri uri) {
int code = sUriMatcher.match(uri);
if (code==3) {
return "vnd.android.cursor.item/contact";
}else if (code==2) {
return "vnd.android.cursor.dir/contacts";
}
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
int code = sUriMatcher.match(uri);
Log.v("meeeeeee", "code="+code);
if (code==1) {
SQLiteDatabase db = mHelper.getReadableDatabase();
return db.query(DbConst.CONTACT_TABLE,
projection,
selection,
selectionArgs,
null,
null,
null);
}else if (code==2) {
SQLiteDatabase db = mHelper.getReadableDatabase();
return db.query(DbConst.CONTACT_TABLE,
null,
null,
null,
null,
null,
null);
}else if (code==3) {
SQLiteDatabase db = mHelper.getReadableDatabase();
List<String> pathSegments = uri.getPathSegments();
String id = pathSegments.get(pathSegments.size()-1);
return db.query(DbConst.CONTACT_TABLE, null, DbConst._ID+"=?", new String[]{id}, null, null, null);
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = mHelper.getReadableDatabase();
int code = sUriMatcher.match(uri);
Log.v("meeeeeee", "code="+code);
long id = db.insert(DbConst.CONTACT_TABLE, null, values);
String str = uri.toString();
str=str+"/"+id;
return Uri.parse(str);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int code = sUriMatcher.match(uri);
Log.v("meeeeeee", "code="+code);
if (code==3) {
List<String> pathsegments = uri.getPathSegments();
String id = pathsegments.get(pathsegments.size()-1);
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.delete(DbConst.CONTACT_TABLE, DbConst._ID+"=?", new String[]{id});
}else if (code==2) {
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.delete(DbConst.CONTACT_TABLE, null,null);
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int code = sUriMatcher.match(uri);
Log.v("meeeeeee", "code="+code);
if (code==3) {
List<String> pathsegments = uri.getPathSegments();
String id = pathsegments.get(pathsegments.size()-1);
Log.v("meeeeeee", "id="+id);
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.update(DbConst.CONTACT_TABLE, values, DbConst._ID+"=?", new String[]{id});
}else if (code==2) {
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.update(DbConst.CONTACT_TABLE, values, null,null);
}
return 888;
}
}
读取通话记录
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void get(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://包名/分类");
Cursor cursor = cr.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex("name"));
}
}
public void insert(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("");
ContentValues values = new ContentValues();
values.put("key", "value");
Uri curosr = cr.insert(uri, values);
}
}
边界压缩
public class MainActivity extends Activity {
ImageView mIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIv=(ImageView)findViewById(R.id.iv);
}
public void readPic(View v){
File file = new File (getFilesDir(),"largeimage.jpg");
Options opts = new Options();
opts.inJustDecodeBounds=true;
WindowManager manager = getWindowManager();
Display display = manager.getDefaultDisplay();
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(),opts);
int sWidth = display.getWidth();
int sHeigh = display.getHeight();
float widthScale = opts.outWidth/sWidth*1.0f;
float heightScale = opts.outHeight/sHeigh*1.0f;
float realScale = 1;
if (widthScale>heightScale&&widthScale>1) {
realScale=widthScale;
}
if (heightScale>widthScale&&heightScale>1) {
realScale=heightScale;
}
opts.inSampleSize=(int)realScale;
opts.inJustDecodeBounds=false;
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(),opts);
Log.v("meeeeeee", bm.getWidth()+" "+ bm.getHeight());
Log.v("meeeeeee", "比例"+realScale);
mIv.setImageBitmap(bm);
}
}
声音池的使用
public class MainActivity extends Activity {
int mSoundId;
private SoundPool mSoundPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSoundPool=new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
mSoundId = mSoundPool.load(this, R.raw.alarm, 1);
}
public void play(View v){
mSoundPool.play(mSoundId, 1.0f, 1.0f, 5, 0, 1.0f);
}
}