(1)添加获取手机联系人的权限
(2) 创建一个javabead.用来存信息
package com.example.day011;
public class Phone {
private String name;
private String number;
public Phone() {
}
public Phone(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Phone{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
'}';
}
}
(3)获得手机联系
package com.example.day011;
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.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button buttonId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonId = findViewById(R.id.button_id);
buttonId.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 = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] strings = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = contentResolver.query(uri, strings, 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(MainActivity.this, name, Toast.LENGTH_SHORT).show();
//有了对象,其他都好说了.
Phone phone = new Phone(name, number);
}
}
}
1,在清单文件中添加授权
2,获得短信内容,展现在ListView中 或者将信息存储到数据库中
uri:Telephony.Sms.CONTENT_URI (请求的地址)
字段:Telephony.Sms.ADDRESS 地址 Telephony.Sms.BODY 内容
private void smsMethod() {
ContentResolver resolver = getContentResolver();
Uri uri= Telephony.Sms.CONTENT_URI;
String[] strs={Telephony.Sms.BODY,Telephony.Sms.ADDRESS};
Cursor cursor=resolver.query(uri,strs,null,null,null);
//第一种ListView 中 第二种 :存到数据库
ArrayList list=new ArrayList<>();
while(cursor.moveToNext()){
String address=cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
String body=cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
Toast.makeText(this, body, Toast.LENGTH_SHORT).show();
list.add(new Msm(address,body));
//执行insert语句
}
}
java中的添加的代码
1,添加一个按钮,在添加一个点击事件
smsId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_SMS,Manifest.permission.SEND_SMS},101);
}else{
smsMethod();
}
}
});
2,在授权回调的方法中,添加相应的逻辑
@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();
}
}
1,添加权限.
2,添加一个点击事件
picId.setOnClickListener(new View.OnClickListener() {
@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() {
//TODO 1:获得ContentResolver
ContentResolver contentResolver = getContentResolver();
//参数一 URI
//TODO 2:获得图库的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));
Log.e("###", "图片: "+title+":"+data+":"+height+":"+width+":"+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();
}
}
1,添加权限.
2,添加一个监听事件
musicId.setOnClickListener(new View.OnClickListener() {
@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() {
//TODO 1:获得ContentResolver
ContentResolver contentResolver = getContentResolver();
//参数一 URI
//TODO 2:获得音乐的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));
Log.i("###", "音乐: "+title+":"+data+":"+artist+":"+duration+":"+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();
}
}
1,添加权限.
2,添加一个监听事件
videoId.setOnClickListener(new View.OnClickListener() {
@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() {
//TODO 1:获得ContentResolver
ContentResolver contentResolver = getContentResolver();
//参数一 URI
//TODO 2:获得视频的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));
Log.i("###", "视频: "+title+":"+data+":"+artist+":"+duration+":"+size);
}
}
4,回调函数中,添加逻辑
else if (requestCode == 104 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
videoMethod();
}
获取图库:(读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 类型清单文件中: 联网的权限:
写SD卡权限:
读SD卡权限:
读取联系人的权限:
读取短信的权限:
读取通话记录的权限:
步骤和以上相同
1,添加一个权限
2,是添加一个点击事件
3.java代码
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);
}
4,在回调事件中,添加方法.
扩展资料
两个博客结合看.
https://www.cnblogs.com/yanganok/p/4584043.html
https://blog.csdn.net/cbk861110/article/details/50930828
1,创建一个观察者
package com.example.day011;
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;
import android.util.Log;
import android.widget.Toast;
/**
* 原理,
* 1,写一个观察者,
* 2,观察者一直在监视的短信的动态
* 3,一旦发现有短信的变化(也就是短信的数据库里的数据有变化)
* 4,就执行相应的业务逻辑
*/
public class SmsObserver extends ContentObserver {
private static final String TAG = "SmsObserver";
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));
Log.i(TAG, "onChange: 0000"+string);
Message obtain = Message.obtain();
obtain.obj = string;
handler.sendMessage(obtain);
}else{
Log.i(TAG, "onChange: kong");
}
}
}
2.Activity中的代码
package com.example.day011;
import android.content.ContentResolver;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class SmsActivity extends AppCompatActivity {
private EditText smsId;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String s = (String)msg.obj;
Toast.makeText(SmsActivity.this, s, Toast.LENGTH_SHORT).show();
smsId.setText(s);
}
};
private SmsObserver smsObserver;
private ContentResolver contentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
smsId = findViewById(R.id.sms_id);
smsObserver = new SmsObserver(handler,this);
contentResolver = getContentResolver();
//注册观察者
contentResolver.registerContentObserver(Telephony.Sms.CONTENT_URI,true,smsObserver);
}
@Override
protected void onDestroy() {
super.onDestroy();
//注销观察者
contentResolver.unregisterContentObserver(smsObserver);
}
}