欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

内容解析者ContentResolver,显示短信

程序员文章站 2024-02-09 16:54:34
...

信息封装类SmsInfo

public class SmsInfo {
    private int _id;                    // 短信的主键
    private String address;           // 发送地址
    private int type;                  // 类型
    private String body;              // 短信内容
    private long date;                // 时间
    // 构造方法
    public SmsInfo(int _id, String address, int type, String body, long date) {
        this._id = _id;
        this.address = address;
        this.type = type;
        this.body = body;
        this.date = date;
    }
    public int get_id() {
        return _id;
    }
    public void set_id(int _id) {
        this._id = _id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public long getDate() {
        return date;
    }
    public void setDate(long date) {
        this.date = date;
    }
}

MainActivity页面


public class MainActivity extends AppCompatActivity {
    private TextView tv_sms;
    private TextView tv_des;
    private String text = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_sms = (TextView) findViewById(R.id.tv_sms);
        tv_des = (TextView) findViewById(R.id.tv_des);
        queryAuthority();
    }
    //请求权限
    private void queryAuthority() {
        int hasPermission = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            hasPermission = checkSelfPermission(Manifest.permission.READ_SMS);
        }
        if (hasPermission != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.READ_SMS}, 123);
            }
            return;
        }
    }

    public void readSMS(View view) {
        text="";
        //查询系统信息的uri
        Uri uri=Uri.parse("content://sms/");
        //获取解析对象
        ContentResolver resolver=getContentResolver();
        //通过ContentResolver对象查询系统短信
        Cursor cursor = resolver.query(uri,new String[]{"_id","address","type","body","date"},null,null,null);
        List<SmsInfo>smsInfos=new ArrayList<SmsInfo>();
        if (cursor!=null&&cursor.getCount()>0){
         tv_des.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";
        }
        tv_sms.setText(text);


    }
}

相关标签: 安卓