当前位置: 代码迷 >> 综合 >> ContentProvider 系统URI
  详细解决方案

ContentProvider 系统URI

热度:61   发布时间:2024-01-17 11:37:45.0

ContentProvider 系统URI

  • 案例一,获取手机联系人,展现在ListView中并存储到数据库中
  • 案例二,获取手机短信
  • 案例三,获取手机所有的图片
  • 案例四,获取手机所有的图片
  • 案例五,获取手机所有的视频
  • 所有权限及接口明细
  • 案例六,手机联系人的增删改
  • 案例七,自动填写注册手机验证码

案例一,获取手机联系人,展现在ListView中并存储到数据库中

(1)添加获取手机联系人的权限

 <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission><uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

(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;}@Overridepublic 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);buttonId = findViewById(R.id.button_id);buttonId.setOnClickListener(new View.OnClickListener() {
    @Overridepublic 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();}}});}@Overridepublic 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,在清单文件中添加授权

 <uses-permission android:name="android.permission.READ_SMS" />

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<Msm> 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() {
    @Overridepublic 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,在授权回调的方法中,添加相应的逻辑

@Overridepublic 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,添加权限.

<!--读写sd卡的权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2,添加一个点击事件

		picId.setOnClickListener(new View.OnClickListener() {
    @Overridepublic 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:获得ContentResolverContentResolver contentResolver = getContentResolver();//参数一 URI//TODO 2:获得图库的URIUri uri1 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//图库的URICursor 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,回调函数中,添加逻辑

@Overridepublic 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,添加权限.

<!--读写sd卡的权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2,添加一个监听事件

       musicId.setOnClickListener(new View.OnClickListener() {
    @Overridepublic 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:获得ContentResolverContentResolver contentResolver = getContentResolver();//参数一 URI//TODO 2:获得音乐的URIUri uri1 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;//音乐的URICursor 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,回调函数中,添加逻辑

    @Overridepublic 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,添加权限.

 <!--读写sd卡的权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2,添加一个监听事件

 videoId.setOnClickListener(new View.OnClickListener() {
    @Overridepublic 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:获得ContentResolverContentResolver contentResolver = getContentResolver();//参数一 URI//TODO 2:获得视频的URIUri 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 类型清单文件中:
联网的权限:<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>读取通话记录的权限:<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>

案例六,手机联系人的增删改

步骤和以上相同
1,添加一个权限
2,是添加一个点击事件
3.java代码

      private void addPerson() {
    ContentResolver contentResolver = getContentResolver();Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");//ContactsContract.RawContacts.CONTENT_URIContentValues 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();//添加Emailvalues.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", "11111111@qq.com");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();}@Overridepublic 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(){
    @Overridepublic 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;@Overrideprotected 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);}@Overrideprotected void onDestroy() {
    super.onDestroy();//注销观察者contentResolver.unregisterContentObserver(smsObserver);}
}
  相关解决方案