使用contentProvider
内部利用contentProvider暴露接口供外部查询删除操作,外部查询删除使用contentResolver,首先使用sqlite创建一个数据库表student,然后使用contentProvider暴露数据,在另一个modle里使用contentResolver解析数据,contentProvider底层是通过Binder实现的
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //contentprovider不用在此创建对象,只要在配置文件里声明就可以执行.输出"创建"证明创建成功,创建只有一次,第二次不会执行oncreate //方法,可以在data/data/包名,中找到数据库文件,删除就会重新创建并执行oncreate方法 } }
上面是主activity
public class Mysqlite extends SQLiteOpenHelper { SQLiteDatabase readableDatabase; public Mysqlite(Context context) { super(context, "student", null, 2); readableDatabase = getWritableDatabase();//创建数据库不需要什么权限,但是这行代码一定要写 } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table student (id integer,name varchar)"); System.out.println("创建"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
上面是创建数据库
public class MyContentProvider extends ContentProvider { Mysqlite mysqlite; SQLiteDatabase sqLiteDatabase; private static final UriMatcher uriMatcher;//一定也要静态的才行 static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI("com.january.summer", "student", 0);//表的第一行 uriMatcher.addURI("com.january.summer", "student", 1); uriMatcher.addURI("com.january.summer", "student", 2); } public MyContentProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int deleteInt = 0; deleteInt = sqLiteDatabase.delete("student", selection, selectionArgs); //参数1:表名 参数2:约束删除列的名字 参数3:具体行的值 switch (uriMatcher.match(uri)) { case 0: deleteInt = sqLiteDatabase.delete("student", selection, selectionArgs); System.out.println("删除第一行"); break;case 1: String deleteId = uri.getPathSegments().get(1); deleteInt = sqLiteDatabase.delete("student", "id=?", new String[]{deleteId}); System.out.println("删除第二行"); break; case 2: sqLiteDatabase.delete("student", selection, selectionArgs); System.out.println("删除第三行"); break; } return deleteInt; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { sqLiteDatabase.insert("student", null, values); getContext().getContentResolver().notifyChange(uri, null); return uri; } @Override public boolean onCreate() { mysqlite = new Mysqlite(getContext()); sqLiteDatabase = mysqlite.getWritableDatabase(); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return sqLiteDatabase.query("student", projection, selection, selectionArgs, null, null, null); //必须自己实现查询功能,外部程序才可以查询 } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; } }
上面是contentProvider
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <provider android:name=".MyContentProvider" android:authorities="com.january.summer" android:enabled="true" android:exported="true"></provider> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
上面是配置文件//
/////////////
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Uri uri = Uri.parse("content://com.january.summer/student"); ContentValues contentValues = new ContentValues(); contentValues.put("name", "小明"); getContentResolver().insert(uri, contentValues); Cursor bookCursor = getContentResolver().query(uri, new String[]{"id", "name"}, null, null, null); int count = bookCursor.getCount(); System.out.println("行数"+count); getContentResolver().delete(uri, "name=?", new String[]{"小明"}); if (bookCursor != null) { bookCursor.moveToFirst(); while (bookCursor.moveToNext()) { System.out.println("ID:" + bookCursor.getInt(bookCursor.getColumnIndex("id")) + " name:" + bookCursor.getString(bookCursor.getColumnIndex("name"))); } bookCursor.close(); } } }
上面是另一个modle的主activity,每运行一次插入一条数据,插入后游标不一定在第一行,movetofirst让游标移动到第一行前面,此时没有数据,movetonext才到第一行.可以用debug测试看的更清楚