内容提供者
内容提供者
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="添加100条数据"
android:onClick="click"/>
<Button
android:id="@+id/insert"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="添加一条数据"
android:onClick="click"/>
<Button
android:id="@+id/update"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="更新数据"
android:onClick="click"/>
<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="删除数据"
android:onClick="click"/>
<Button
android:id="@+id/query"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="查询数据"
android:onClick="click"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
MySQLiteOpenHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建一个表
String sql = "create table kangxiaobo(name varchar(20))";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MyContentProvide
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase database;
@Override
public boolean onCreate() {
//指定对外开放的数据库并获得数据库操作类
database = new MySQLiteOpenHelper(getContext(), "kangxiaobo", null, 1).getWritableDatabase();
if (database == null) {
return false;
}
return true;
}
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
/***
* @param table "kangxiaobo" --> 相当于select *** from table语句中的table。如果是多表联合查询,可以用逗号将两个表名分开
* @param columns string --> 要查询出来的列名。相当于select *** from table语句中的 ***部分。如果是查询多个参数,可以用逗号将两个参数分开
* @param selection s --> 查询条件子句,相当于select *** from table where && 语句中的&&部分,在条件子句允许使用占位符“?”表示条件值
* @param selectionArgs string1 --> 对应于selection参数占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常
* @param groupBy --> 相当于select *** from table where && group by ...语句中...的部分
* @param having --> 相当于select *** from table where && group by ...having %%%语句中%%%的部分, 作用于groupBy的条件
* @param olderBy s1 --> 排序
* @param limit --> 指定偏移量和获取的记录数,相当于select语句limit关键字后面的部分
* @return 游标
*/
return database.query("kangxiaobo", strings, s, strings1, null, null, s1);
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
/***
* @param table "kangxiaobo" --> 表名
* @param nullColumnHack --> 默认值
* @param values contentValues --> 要插入的数据
*/
database.insert("kangxiaobo",null,contentValues);
return uri;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
/***
* 执行删除的方法
* @param table "kangxiaobo" --> 表名
* @param s whereClause s --> 删除的条件
* @param whereArgs strings --> 条件中?对应的值
*/
return database.delete("kangxiaobo",s,strings);
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
/***
* @param table "kangxiaobo" --> 表名
* @param values contentValues --> 更新的数据
* @param whereClause s --> 更新的条件
* @param whereArgs strings --> 条件中?对应的值
*/
return database.update("ji", contentValues,s , strings);
}
}
清单文件注册
<provider
android:exported="true"
android:authorities="com.kangxiaobo"
android:name=".MyContentProvider"/>
MainActivity
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private MySQLiteOpenHelper sqLiteOpenHelp;
private ContentResolver contentResolver;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
sqLiteOpenHelp = new MySQLiteOpenHelper(this, "kangxiaobo", null, 1);
//获得内容解析器
contentResolver = getContentResolver();
//获得Uri com.kangxiaobo是在清单文件中写的 android:authorities="com.kangxiaobo"
uri = Uri.parse("content://com.kangxiaobo");
}
public void click(View view) {
switch (view.getId()) {
case R.id.add:
add();
query();
break;
case R.id.insert:
insert();
query();
break;
case R.id.update:
update();
query();
break;
case R.id.delete:
delete();
query();
break;
case R.id.query:
query();
break;
}
}
private void query() {
Cursor cursor = contentResolver.query(uri, null, null, null, null);
List<String> data = new ArrayList<>();
while (cursor.moveToNext()) {
data.add(cursor.getString(cursor.getColumnIndex("name")));
}
textView.setText(data + "");
}
private void delete() {
contentResolver.delete(uri, "name like ?", new String[]{"%数据%"});
}
private void update() {
//创建要更新的数据
ContentValues values = new ContentValues();
values.put("name", "我是更新的数据");
contentResolver.update(uri, values, "name like?", new String[]{"康小博%"});
}
private void insert() {
//创建要插入的数据
ContentValues values = new ContentValues();
values.put("name", "我是插入的数据");
//插入数据
contentResolver.insert(uri, values);
}
/***
* 添加100条数据
*/
private void add() {
//获取数据库的操作类
SQLiteDatabase database = sqLiteOpenHelp.getWritableDatabase();
try {
//开启一次事务
database.beginTransaction();
//添加数据
for (int i = 0; i < 100; i++) {
database.execSQL("insert into kangxiaobo values(?)", new String[]{"康小博" + i + "号"});
}
database.setTransactionSuccessful();
} catch (RuntimeException e) {
Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
} finally {
//事务结束
database.endTransaction();
//关闭数据库操作类
database.close();
}
}
}
读取系统的内容
MainActivity类
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
getAccess(Manifest.permission.READ_CONTACTS);
}
//读取联系人
private void readUser() {
Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] phoneProjection = {ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
"phonebook_label"};
ContentResolver contentResolver = getContentResolver();
//从联系人的数据库中查找姓名,电话,首字母,并按照首字母排序
Cursor cursor = contentResolver.query(phoneUri, phoneProjection, null, null, "phonebook_label asc");
List<String> list = new ArrayList<>();
//获取游标内的数据 并存入集合
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
list.add(name + number);
}
//设置适配器
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list));
Log.i("##", "设置类适配器");
}
private void getAccess(String permission) {
//判断Android是否是6.0以上的版本
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
//判断是否已经获得了权限
if (ContextCompat.checkSelfPermission(getApplicationContext(), permission) == 0) {
return;
}
//请求权限
requestPermissions(new String[]{permission}, 1);
}
//查看联系人的监听
public void readUser(View view) {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
readUser();
} else {
Toast.makeText(this, "没有权限", Toast.LENGTH_SHORT).show();
}
}
}