手机系统 | URI |
---|---|
获取图库(读sd卡的权限) | MediaStore.Images.Media.EXTERNAL_CONTENT_URI |
获取视频(读sd卡的权限) | MediaStore.Video.Media.EXTERNAL_CONTENT_URI |
获取音频(读sd卡的权限) | MediaStore.Audio.Media.EXTERNAL_CONTENT_URI |
获取联系人(读联系人的权限) | ContactsContract.CommonDataKinds.Phone.CONTENT_URI |
获取短信(读短信的权限) | Telephony.Sms.CONTENT_URI |
通话记录 | CallLog.Calls.CONTENT_URI |
权限 | 清单文件 |
---|---|
联网权限 | uses-permission android:name=“android.permission.INTERNET” |
写SD卡权限 | uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” |
读SD卡权限 | uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE” |
读取联系人权限 | uses-permission android:name=“android.permission.READ_CONTACTS” |
读取短信权限 | uses-permission android:name=“android.permission.READ_SMS” |
读取通话记录权限 | uses-permission android:name=“android.permission.READ_CALL_LOG” |
内容参数:
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 姓名
ContactsContract.CommonDataKinds.Phone.NUMBER}; 电话
1、添加联系人权限
<!-- 添加获取手机联系人权限 -->
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2、java代码
btnLinkman.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//动态授权(6.0及以上)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS}, 100);
} else {
resolverMethod();
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==100&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
resolverMethod();
}
}
//从数据库获取手机联系人,因为要重用,所以封装成方法
private void resolverMethod() {
//获取内容解析者
ContentResolver contentResolver = getContentResolver();
//设置手机联系人的uri
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] strs = new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = contentResolver.query(uri, strs, null, null, 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));
Toast.makeText(this, name+"+"+number, Toast.LENGTH_SHORT).show();
//将数据存入对象,便于后续操作
// JavaBean javaBean = new JavaBean(name,number);
}
}
内容参数:
Telephony.Sms.ADDRESS, 地址
Telephony.Sms.BODY}; 内容
1、添加授权
<uses-permission android:name="android.permission.READ_SMS" />
2、java代码
动态授权同上
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_SMS,Manifest.permission.SEND_SMS},101);
} else{
smsMethod();
}
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
Uri uri = Telephony.Sms.CONTENT_URI;
String[] strs={Telephony.Sms.BODY,Telephony.Sms.ADDRESS};
Cursor cursor=contentResolver.query(uri,strs,null,null,null);
while(cursor.moveToNext()){
String address=cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
String body=cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
Toast.makeText(MainActivity.this, address+"--"+body, }
}
});
3、在授权回调的方法中,添加相应的逻辑
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
resolverMethod();
}else if (requestCode == 101 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
smsMethod();
}
}
内容参数:
MediaStore.Images.Media.TITLE; 图片名称
MediaStore.Images.Media.SIZE; 大小
MediaStore.Images.Media.DATA; 地址
MediaStore.Images.Media.HEIGHT; 高
MediaStore.Images.Media.WIDTH; 宽
1、添加权限
<!--读写sd卡的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2、点击事件
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},102);
}else{
picMethod();
}
}
});
3、java代码
private void picMethod() {
//获得ContentResolver
ContentResolver contentResolver = getContentResolver();
//获得图库的URI
Uri uri1 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//图库的URI
Cursor cursor = contentResolver.query(uri1, null, null, null, null);
while (cursor.moveToNext()){
//字段
String title=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
//图片的地址
String data=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
int height=cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media.HEIGHT));
int width=cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media.WIDTH));
long size=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
}
}
4、回调函数中,添加逻辑
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
resolverMethod();
}else if (requestCode == 101 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
smsMethod();
}else if (requestCode == 102 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
picMethod();
}
}
内容参数:
MediaStore.Audio.Media.TITLE, 歌名
MediaStore.Audio.Media.ARTIST, 歌手
MediaStore.Audio.Media.DURATION, 总时长
MediaStore.Audio.Media.DATA, 地址
MediaStore.Audio.Media.SIZE, 大小
MediaStore.Audio.Media.ALBUM_ID, 专辑封面的id
1、添加权限
<!--读写sd卡的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2、点击事件
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},103);
}else{
musicMethod();
}
}
});
3、java代码
private void musicMethod() {
//获得ContentResolver
ContentResolver contentResolver = getContentResolver();
//获得音乐的URI
Uri uri1 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;//音乐的URI
Cursor cursor = contentResolver.query(uri1, null, null, null, null);
while (cursor.moveToNext()){
//字段
String title=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
//地址
String data=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
//歌手
String artist=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
//时长
long duration=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
//大小
long size=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
}
}
4、回调函数中,添加逻辑
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
resolverMethod();
}else if (requestCode == 101 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
smsMethod();
}else if (requestCode == 102 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
picMethod();
}else if (requestCode == 103 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
musicMethod();
}
}
内容参数:
MediaStore.Video.Media.TITLE, 歌名
MediaStore.Video.Media.ARTIST, 歌手
MediaStore.Video.Media.DURATION, 总时长
MediaStore.Video.Media.DATA, 地址
MediaStore.Audio.Media.SIZE}; 大小
1、添加权限
<!--读写sd卡的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2、添加监听事件
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},104);
}else{
videoMethod();
}
}
});
3、java代码
private void videoMethod() {
//获得ContentResolver
ContentResolver contentResolver = getContentResolver();
//获得视频的URI
Uri uri1 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;//视频的URI
// Uri uri1 = Uri.parse("content://media/external/video/media");
Cursor cursor = contentResolver.query(uri1, null, null, null, null);
while (cursor.moveToNext()){
//字段
String title=cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
String data=cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
String artist=cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.ARTIST));
long duration=cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
long size=cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
}
}
4、回调函数,添加逻辑
else if (requestCode == 104 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
videoMethod();
}
private void addPerson() {
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
//ContactsContract.RawContacts.CONTENT_URI
ContentValues values = new ContentValues();
//插入一条空数据,返回当前空数据的id.
long contactid = ContentUris.parseId(contentResolver.insert(uri, values));
uri = Uri.parse("content://com.android.contacts/data");
//添加姓名
values.put("raw_contact_id", contactid);
values.put("mimetype", "vnd.android.cursor.item/name");
values.put("data1", "xiaoming11");
contentResolver.insert(uri, values);
values.clear();
//添加电话
values.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, contactid);
//ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
values.put(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/phone_v2");
//ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE;
values.put(ContactsContract.Data.DATA1, "12312312311");
contentResolver.insert(uri, values);
values.clear();
//添加Email
values.put("raw_contact_id", contactid);
//ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE;
values.put(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/email_v2");
values.put("data1", "[email protected]");
contentResolver.insert(uri, values);
}
1、创建一个观察者
/**
* 创建一个观察者
* 1,写一个观察者,
* 2,观察者一直在监视的短信的动态
* 3,一旦发现有短信的变化(也就是短信的数据库里的数据有变化)
* 4,就执行相应的业务逻辑
*/
public class SmsObserver extends ContentObserver {
private ContentResolver contentResolver;
private Handler handler;
public SmsObserver(Handler handler, Context context) {
super(handler);
this.handler = handler;
contentResolver = context.getContentResolver();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
String[] strs={Telephony.Sms.BODY,Telephony.Sms.ADDRESS};
Cursor cursor = contentResolver.query(Telephony.Sms.CONTENT_URI, strs, null, null, null);
if (cursor!=null){
//获取第一个游标位置
cursor.moveToFirst();
String string = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
//将获取到的数据发送给Activity
Message obtain = Message.obtain();
obtain.obj = string;
handler.sendMessage(obtain);
}
}
}
2、通过观察者将信息发送到EditText
public class Main2Activity extends AppCompatActivity {
private EditText etSms;
private SmsObserver smsObserver;
private ContentResolver contentResolver;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
String s = (String) msg.obj;
etSms.setText(s);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
etSms = (EditText) findViewById(R.id.et_sms);
//动态请求
requestPermissions(new String[]{Manifest.permission.READ_SMS},110);
smsObserver = new SmsObserver(handler,this);
contentResolver = getContentResolver();
//注册内容观察者
contentResolver.registerContentObserver(Telephony.Sms.CONTENT_URI,true,smsObserver);
}
@Override
protected void onDestroy() {
super.onDestroy();
//注销观察者
contentResolver.unregisterContentObserver(smsObserver);
}
}