获取系统ContentProvider的例子(获取联系人及手机号)
public class MainActivity extends AppCompatActivity {
private Button button;
private ListView listView;
private final int CAMERA_REQUEST_CODE = 1;
private List<String> list = new ArrayList<>();
private ArrayAdapter<String> arrayAdapter;
private void requestPermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CONTACTS)) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("申请打电话权限")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS}, CAMERA_REQUEST_CODE);
}
})
.show();
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS}, CAMERA_REQUEST_CODE);
}
} else {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestPermission();
setContentView(R.layout.activity_main);
arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
button = (Button) findViewById(R.id.button);
listView = (ListView) findViewById(R.id.list);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
duqu();
listView.setAdapter(arrayAdapter);
}
});
}
public void duqu() {
Cursor cursor = null;
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null) {
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);
}
}
arrayAdapter.notifyDataSetChanged();//刷新适配器
if (cursor != null) {
cursor.close();
}
}
}
自己创建的contentprovider,代码如下:
在provider.java文件中
public class provider extends ContentProvider {
public static final int BOOK_DIR=0;
public static final int BOOK_ITEM=1;
public static final String AUTHORITY="com.example.liang.lianxi";
private static UriMatcher uriMatcher;
private shujuku shujuku;
static {
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
}
@Override
public boolean onCreate() {
shujuku=new shujuku(getContext());
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=shujuku.getReadableDatabase();
Cursor cursor=null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
cursor=db.query("book",projection,selection,selectionArgs,null,null,sortOrder);
break;
case BOOK_ITEM:
String id=uri.getPathSegments().get(1);
cursor=db.query("book",projection,"id=?",new String[]{id},null,null,sortOrder);
break;
default:
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.liang.lianxi.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.liang.lianxi.book";
}
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase sqLiteDatabase=shujuku.getWritableDatabase();
Uri h=null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
case BOOK_ITEM:
long l=sqLiteDatabase.insert("book",null,values);
h=Uri.parse("content://"+AUTHORITY+"/book/"+l);
break;
default:
}
return h;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase sqLiteDatabase=shujuku.getReadableDatabase();
switch (uriMatcher.match(uri)){
case BOOK_DIR:
sqLiteDatabase.delete("book",selection,selectionArgs);
break;
case BOOK_ITEM:
String bookid=uri.getPathSegments().get(1);
sqLiteDatabase.delete("book","id=?",new String[]{bookid});
break;
default:
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase sqLiteDatabase=shujuku.getWritableDatabase();
switch (uriMatcher.match(uri)){
case BOOK_DIR:
sqLiteDatabase.update("book",values,selection,selectionArgs);
break;
case BOOK_ITEM:
String id=uri.getPathSegments().get(1);
sqLiteDatabase.update("book",values,"id=?",new String[]{id});
break;
default:
}
return 0;
}
}
在shujuku.java文件中
public class shujuku extends SQLiteOpenHelper {
public shujuku(Context context) {
super(context, "tushuguan.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String kebiaoSQL = "CREATE TABLE book( "+"id int primarykey,name varchar(15) ,author varchar(15) ,price int,pages int)";
db.execSQL(kebiaoSQL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
在Book.java文件中
public class Book{
private int id;
private String author;
private int price;
private int pages;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在androidmanifest.xml文件中
...
<provider
android:name=".provider"
android:authorities="com.example.liang.lianxi"
android:enabled="true"
android:exported="true" />
...
在MainActivity.java文件中
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
在content resolver方,代码如下:
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button1;
private Button button2;
private Button button3
;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.button);
button1= (Button) findViewById(R.id.button1);
button2= (Button) findViewById(R.id.button2);
button3= (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri=Uri.parse("content://com.example.liang.lianxi/book");
ContentValues contentValues=new ContentValues();
contentValues.put("name","西游记");
contentValues.put("author","吴承恩");
contentValues.put("pages",300);
contentValues.put("price",40);
Uri newuri=getContentResolver().insert(uri,contentValues);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri=Uri.parse("content://com.example.liang.lianxi/book");
Cursor cursor=getContentResolver().query(uri,null,null,null,null);
if(cursor!=null){
while (cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
int price=cursor.getInt(cursor.getColumnIndex("price"));
Log.i("xinxi",name+","+author+","+pages+","+price);
}
}
cursor.close();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri=Uri.parse("content://com.example.liang.lianxi/book");
ContentValues contentValues=new ContentValues();
contentValues.put("name","水浒传");
contentValues.put("author","施耐庵");
getContentResolver().update(uri,contentValues,null,null);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri=Uri.parse("content://com.example.liang.lianxi/book");
getContentResolver().delete(uri,null,null);
}
});
}
}