一、排坑
短信操作崩溃
编码的转换问题,短信中的那种表情(自带的除外)不能正常的转换utf-8格式字符,导致备份失败,
二、内容提供者
- 应用的数据库是不允许其他应用访问的
- 内容提供者的作用就是让别的应用访问到你的数据库
-
自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代码,
- 在清单文件中定义内容提供者的标签,注意必须要有authorities属性,这是内容提供者的主机名,功能类似地址
- 创建一个其他应用,访问自定义的内容提供者,实现对数据库的插入操作
- 通过Uri匹配器可以实现操作不同的表
提供者
public class PersonProvider extends ContentProvider { private MyOpenHelper oh; SQLiteDatabase db; //创建uri匹配器对象 static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH); //检测其他用户传入的uri与匹配器定义好的uri中,哪条匹配 static { um.addURI("com.itheima.people", "person", 1);//content://com.itheima.people/person um.addURI("com.itheima.people", "teacher", 2);//content://com.itheima.people/teacher um.addURI("com.itheima.people", "person/#", 3);//content://com.itheima.people/person/4 } //内容提供者创建时调用 @Override public boolean onCreate() { oh = new MyOpenHelper(getContext()); db = oh.getWritableDatabase(); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = null; if(um.match(uri) == 1){ cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder, null); } else if(um.match(uri) == 2){ cursor = db.query("teacher", projection, selection, selectionArgs, null, null, sortOrder, null); } else if(um.match(uri) == 3){ //把uri末尾携带的数字取出来 long id = ContentUris.parseId(uri); cursor = db.query("person", projection, "_id = ?", new String[]{id + ""}, null, null, sortOrder, null); } else{ throw new IllegalArgumentException("uri又有问题哟亲么么哒"); } return cursor; } @Override public String getType(Uri uri) { if(um.match(uri) == 1){ return "vnd.android.cursor.dir/person"; } else if(um.match(uri) == 3){ return "vnd.android.cursor.item/person"; } return null; } //此方法供其他应用调用,用于往people数据库里插数据 //values:由其他应用传入,用于封装要插入的数据 //uri:内容提供者的主机名,也就是地址 @Override public Uri insert(Uri uri, ContentValues values) { //使用uri匹配器匹配传入的uri if(um.match(uri) == 1){ db.insert("person", null, values); //发送数据改变的通知 //uri:通知发送到哪一个uri上,所有注册在这个uri上的内容观察者都可以收到这个通知 getContext().getContentResolver().notifyChange(uri, null); } else if(um.match(uri) == 2){ db.insert("teacher", null, values); getContext().getContentResolver().notifyChange(uri, null); } else{ throw new IllegalArgumentException("uri有问题哟亲么么哒"); } return uri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int i = db.delete("person", selection, selectionArgs); return i; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int i = db.update("person", values, selection, selectionArgs); return i; }}
public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "people.db", null, 2); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person(_id integer primary key autoincrement, name varchar(10), money integer(20))"); db.execSQL("create table teacher(_id integer primary key autoincrement, name varchar(10))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("create table teacher(_id integer primary key autoincrement, name varchar(10))"); }}
访问者
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void insert(View v){ //通过内容提供者把数据插入people数据库 //拿到contentResolver ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "赵帅哥");// values.put("money", "13000"); //url:内容提供者的主机名 //values:要插入的数据 cr.insert(Uri.parse("content://com.itheima.people/teacher"), values); } public void delete(View v){ ContentResolver cr = getContentResolver(); int i = cr.delete(Uri.parse("content://com.itheima.people"), "name = ?", new String[]{"小志"}); System.out.println(i); } public void update(View v){ ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "sb志"); int i = cr.update(Uri.parse("content://com.itheima.people"), values, "name = ?", new String[]{"大志"}); System.out.println(i); } public void select(View v){ ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(Uri.parse("content://com.itheima.people/person/4"), null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(1); String money = cursor.getString(2); System.out.println(name + ";" + money); } }}