xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenshuai.myapplication.ActivityContentProvider" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询数据" android:onClick="chaxun_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加数据" android:onClick="tianjia_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="更新数据" android:onClick="gengxin_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除数据" android:onClick="shanchu_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取联系人信息" android:onClick="duqu_onclick"/> </LinearLayout>
JAVA
package com.example.chenshuai.myapplication;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class ActivityContentProvider extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_content_provider);
/*
Intent intent = new Intent(Intent.ACTION_DIAL);
Uri uri = Uri.parse("tel:110");
intent.setData(uri);*/
}
public void chaxun_onclick(View view)
{
//获得解析器
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number");
/*contentResolver.update(uri,new ContentValues(),"",new String[]{});
Toast.makeText(ActivityContentProvider.this, "调用内容提供者", Toast.LENGTH_SHORT).show();*/
Cursor cursor = contentResolver.query(uri, null, null, null, null);
while (cursor.moveToNext())
{
Toast.makeText(ActivityContentProvider.this, "遍历数据:_id = "+cursor.getLong(0)+
"phone_number= "+cursor.getString(1), Toast.LENGTH_SHORT).show();
}
cursor.close();
}
public void tianjia_onclick(View view)
{
//获得解析器
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number");
/*contentResolver.update(uri,new ContentValues(),"",new String[]{});
Toast.makeText(ActivityContentProvider.this, "调用内容提供者", Toast.LENGTH_SHORT).show();*/
ContentValues c= new ContentValues();
c.put("phone_number","123456");
uri = contentResolver.insert(uri,c);
long id = ContentUris.parseId(uri);
Toast.makeText(ActivityContentProvider.this, "新数据的id="+id, Toast.LENGTH_SHORT).show();
}
public void gengxin_onclick(View view)
{
//获得解析器
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number");
/*contentResolver.update(uri,new ContentValues(),"",new String[]{});
Toast.makeText(ActivityContentProvider.this, "调用内容提供者", Toast.LENGTH_SHORT).show();*/
ContentValues c= new ContentValues();
c.put("phone_number","123456");
int content = contentResolver.update(uri, c, null, null);
Toast.makeText(ActivityContentProvider.this, "返回修改的数据?"+content, Toast.LENGTH_SHORT).show();
}
public void shanchu_onclick(View view)
{
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.cheshuai.test.activityhhh1/black_number");
int btn = contentResolver.delete(uri, "_id>?", new String[]{"1"});
Toast.makeText(ActivityContentProvider.this, "删除数据条数"+btn, Toast.LENGTH_SHORT).show();
}
public void duqu_onclick(View view)
{
ContentResolver contentResolver = getContentResolver();
//联系人信息的URI 授权
//管理联系人的Uri
//ContactsContract.Contacts.CONTENT_URI
//管理联系人电话的Uri
//ContactsContract.CommonDataKinds.Phone.CONTENT_URI
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null, null);
while(cursor.moveToNext())
{
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(ActivityContentProvider.this, "数据id= "+id + "数据name= "+name, Toast.LENGTH_SHORT).show();
//通过id查询联系人的电话信息
Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id +""},null);
while (cursor1.moveToNext())
{
String phone = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(ActivityContentProvider.this, "电话信息= "+ phone, Toast.LENGTH_SHORT).show();
}
cursor1.close();
}
cursor.close();
}
}
manifest
<!-- 读取联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <provider android:name=".MyContentProvider" android:authorities="com.example.cheshuai.test.activityhhh1" android:enabled="true" android:exported="true" />