首先我们需要添加权限在下面找到获取手机联系人的权限
内容提供者
获取手机联系人
权限:
uri: ContactsContract.CommonDataKinds.Phone.CONTENT_URI
字段:ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME:姓名
ContactsContract.CommonDataKinds.Phone.NUMBER:电话
获取手机短信
权限:
uri: Telephony.Sms.CONTENT_URI
字段:Telephony.Sms.ADDRESS 地址
Telephony.Sms.BODY 内容
获取音频
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 读外部存储权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
uri:MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
字段:MediaStore.Audio.Media.TITLE歌名
MediaStore.Audio.Media.ARTIST歌手
MediaStore.Audio.Media.DURATION总时长
MediaStore.Audio.Media.DATA地址
获取视频
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 读外部存储权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
uri:MediaStore.Video.Media.EXTERNAL_CONTENT_URI
字段:MediaStore.Video.Media.TITLE歌名
MediaStore.Video.Media.ARTIST歌手
MediaStore.Video.Media.DURATION总时长
MediaStore.Video.Media.DATA地址
获取图库:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 读外部存储权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
uri:MediaStore.Images.Media.EXTERNAL_CONTENT_URI
字段:MediaStore.Images.Media.TITLE;图片名称
MediaStore.Images.Media.SIZE;大小
MediaStore.Images.Media.DATA;地址
广播接收者
1.权限
<uses-permission android:name="android.permission.RECEIVE_SMS" />
action:android.provider.Telephony.SMS_RECEIVED"
打电话权限
action:android.intent.action.NEW_OUTGOING_CALL
android.intent.action.BOOT_COMPLETED
添加完权限之后
开始下面的内容索要方的操作
public void click(View view){
//获得索要方
ContentResolver contentResolver = getContentResolver();
//uri
Uri uri= ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] str={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
//url ,列数组,条件,条件值,排序
Cursor cur = contentResolver.query(uri, str, null, null, null);
ArrayList<phone> arr = new ArrayList<>();
while(cur.moveToNext()){
//获取联系人姓名
String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//获取手机号
String call = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phone phone = new phone();
phone.name=name;
phone.call=call;
arr.add(phone);
//可以在控制台打印一下
}
}
class phone{
String name;
String call;
@Override
public String toString() {
return "phone{" +
"name='" + name + '\'' +
", call='" + call + '\'' +
'}';
}
}
内容提供方
继承ContentProvider,重写他的6个方法,获得SQLitOpenHelper方法
获得数据库对象,getwritebleDatabase
清单文件注册
<provider
android:name=“文件包名”
android:authorties=“uri字符串”
android:enable=“true” 可编辑
android exproted=“true” 对外开放
/provider>
public class MyContentProvider extends ContentProvider {
private MyHelper myHelper;
private SQLiteDatabase db;
@Override
public boolean onCreate() {
myHelper=new MyHelper(getContext());
db=myHelper.getWritableDatabase();
if(db==null) {
return false;
}
return true;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
return db.delete("custom_six",selection,selectionArgs);
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
db.insert("custom_six",null,values);
return uri;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return db.query("custom_six",projection,selection,selectionArgs,null,null,sortOrder);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return db.update("custom_six",values,selection,selectionArgs);
}
}
//项目2中操作
private ContentResolver contentResolver;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uri=Uri.parse("content://MyContentProvider");
contentResolver=getContentResolver();
}