Android之通过ContentProvider读取信息

实操:1、准备数据,首先在模拟器中发送一些测试短信,如下图
Android之通过ContentProvider读取信息_第1张图片
2、了解系统短信内容提供者的URI
Android之通过ContentProvider读取信息_第2张图片
3、查看系统短信的数据库文件
Android之通过ContentProvider读取信息_第3张图片
4、创建工程,拷贝资源,编写界面



    
    
    
    

drawable/目录下的图片资源:
Android之通过ContentProvider读取信息_第4张图片
5、编写实体类,对接数据库信息(关注什么内容,就定义对应的成员, 没有必要全部定义)
Android之通过ContentProvider读取信息_第5张图片
6、编写主逻辑

package com.example.readsms;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private TextView tvSms;
    private TextView tvDes;
    private String text = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvSms = (TextView) findViewById(R.id.tv_sms);
        tvDes = (TextView) findViewById(R.id.tv_des);
    }
    //点击Button时触发的方法
    public void readSMS(View view) {
//查询系统信息的uri
        Uri uri = Uri.parse("content://sms/");
//获取ContentResolver对象
        ContentResolver resolver = getContentResolver();
//通过ContentResolver对象查询系统短信
        Cursor cursor = resolver.query(uri, new String[]{ "_id","address",
                "type","body", "date"}, null, null, null);
        List smsInfos = new ArrayList();
        if (cursor != null && cursor.getCount() > 0) {
            tvDes.setVisibility(View.VISIBLE);
            while (cursor.moveToNext()) {
                int _id = cursor.getInt(0);
                String address = cursor.getString(1);
                int type = cursor.getInt(2);
                String body = cursor.getString(3);
                long date = cursor.getLong(4);
                SmsInfo smsInfo = new SmsInfo(_id, address, type, body, date);
                smsInfos.add(smsInfo);
            }
            cursor.close();
        }
//将查询到的短信内容显示到界面上
        for (int i = 0; i < smsInfos.size(); i++) {
            text += "手机号码:" + smsInfos.get(i).getAddress() + "\n";
            text += "短信内容:" + smsInfos.get(i).getBody() + "\n\n";
            tvSms.setText(text);
        }
    }
}

Android之通过ContentProvider读取信息_第6张图片
8、添加权限,重新测试,问题依然,打开模拟器权限后,一切正常

package com.example.readsms;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private TextView tvSms;
    private TextView tvDes;
    private String text = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvSms = (TextView) findViewById(R.id.tv_sms);
        tvDes = (TextView) findViewById(R.id.tv_des);
    }
    //点击Button时触发的方法
    public void readSMS(View view) {
//查询系统信息的uri
        Uri uri = Uri.parse("content://sms/");
//获取ContentResolver对象
        ContentResolver resolver = getContentResolver();
//通过ContentResolver对象查询系统短信
        Cursor cursor = resolver.query(uri, new String[]{ "_id","address",
                "type","body", "date"}, null, null, null);
        List smsInfos = new ArrayList();
        if (cursor != null && cursor.getCount() > 0) {
            tvDes.setVisibility(View.VISIBLE);
            while (cursor.moveToNext()) {
                int _id = cursor.getInt(0);
                String address = cursor.getString(1);
                int type = cursor.getInt(2);
                String body = cursor.getString(3);
                long date = cursor.getLong(4);
                SmsInfo smsInfo = new SmsInfo(_id, address, type, body, date);
                smsInfos.add(smsInfo);
            }
            cursor.close();
        }
//将查询到的短信内容显示到界面上
        for (int i = 0; i < smsInfos.size(); i++) {
            text += "手机号码:" + smsInfos.get(i).getAddress() + "\n";
            text += "短信内容:" + smsInfos.get(i).getBody() + "\n\n";
            tvSms.setText(text);
        }
    }
}

Android之通过ContentProvider读取信息_第7张图片

你可能感兴趣的:(Android之通过ContentProvider读取信息)