ContentResolver
ContentResolver获取手机联系人
添加权限
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
package com.example.day10;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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.Button;
public class MainActivity extends AppCompatActivity {
private Button but;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.but);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, 100);
}
}
@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) {
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resolverMethod();
}
});
}
}
private void resolverMethod() {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
String[] strings = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor query = contentResolver.query(uri, strings, null, null, null);
while (query.moveToNext()) {
String number = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.i(TAG, "onRequestPermissionsResult: ----" + name + "---" + number);
}
}
}
利用观察者读取短信验证码
package com.example.day10;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
public class SmsObserver extends ContentObserver {
private Handler handler;
private ContentResolver contentResolver;
public SmsObserver(Handler handler, Context context) {
super(handler);
this.handler = handler;
contentResolver = context.getContentResolver();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
String[] strings = {Telephony.Sms.ADDRESS, Telephony.Sms.BODY};
Cursor query = contentResolver.query(Telephony.Sms.CONTENT_URI, strings, null, null, null);
if (query !=null){
query.moveToFirst();
String string = query.getString(query.getColumnIndex(Telephony.Sms.BODY));
Message obtain = Message.obtain();
obtain.obj =string;
handler.sendMessage(obtain);
}
}
}
Activity代码
package com.example.day10;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
String string = (String) msg.obj;
Toast.makeText(Main2Activity.this, string, Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
SmsObserver smsObserver = new SmsObserver(handler, Main2Activity.this);
ContentResolver contentResolver = getContentResolver();
contentResolver.registerContentObserver(Telephony.Sms.CONTENT_URI, true, smsObserver);
}
}
手机的权限
-
获取图库:(读sd卡的权限)
- (1)uri:MediaStore.Images.Media.EXTERNAL_CONTENT_URI
- (2)字段:
MediaStore.Images.Media.TITLE; 图片名称
MediaStore.Images.Media.SIZE; 大小
MediaStore.Images.Media.DATA; 地址
MediaStore.Images.Media.HEIGHT; 高
MediaStore.Images.Media.WIDTH; 宽
-
获取视频:(读sd卡的权限)
- (1)uri:MediaStore.Video.Media.EXTERNAL_CONTENT_URI
- (2)字段:TITLE:歌名 ARTIST 歌手 DURATION 总时长 DATA 地址 SIZE 大小
- String[] strs= new String[]{
MediaStore.Video.Media.TITLE, 歌名
MediaStore.Video.Media.ARTIST, 歌手
MediaStore.Video.Media.DURATION, 总时长
MediaStore.Video.Media.DATA, 地址
MediaStore.Audio.Media.SIZE}; 大小
*获取音频 : (读sd卡的权限)
* (1)uri:MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
* (2)字段:TITLE:歌名 ARTIST 歌手 DURATION 总时长 DATA 地址 SIZE 大小
* String[] strs= new String[]{
MediaStore.Audio.Media.TITLE, 歌名
MediaStore.Audio.Media.ARTIST, 歌手
MediaStore.Audio.Media.DURATION, 总时长
MediaStore.Audio.Media.DATA, 地址
MediaStore.Audio.Media.SIZE}; 大小
*获取联系人: (读联系人的权限)
* (1)uri:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
* (2)字段:Phone.DISPLAY_NAME 姓名 NUMBER 电话
* String[] strs= new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 姓名
ContactsContract.CommonDataKinds.Phone.NUMBER}; 电话
*获取短信: (读短信的权限)
* (1)uri:Telephony.Sms.CONTENT_URI
* (2)字段:ADDRESS 地址 BODY 内容
* String[] strs= new String[]{
Telephony.Sms.ADDRESS, 地址
Telephony.Sms.BODY}; 内容
*通话记录:
* (1)uri:CallLog.Calls.CONTENT_URI
* (2)字段:
NUMBER:电话号码
DATE:时间 毫秒
DURATION:总时长 秒
TYPE 类型
“Accept-Encoding”, “identity”
清单文件中:
联网的权限:
写SD卡权限:
读SD卡权限:
读取联系人的权限:
读取短信的权限:
读取通话记录的权限:
上一篇: 《软件构造》Lab 2
下一篇: 软件构造博客(2)
推荐阅读
-
android利用ContentResolver访问者获取手机短信信息
-
android利用ContentResolver访问者获取手机联系人信息
-
android利用ContentResolver访问者获取手机短信信息
-
android利用ContentResolver访问者获取手机联系人信息
-
Android中ContentProvider和ContentResolver详解
-
android ContentResolver获取手机电话号码和短信内容
-
简单讲讲android 如何使用contentResolver
-
ContentProvider和ContentResolver的使用
-
Android中ContentProvider和ContentResolver详解
-
android ContentResolver获取手机电话号码和短信内容