Android使用ContentProvider实现查看系统短信功能

本文实例为大家分享了使用ContentProvider实现查看系统短信功能的具体代码,供大家参考,具体内容如下

activity_main.xml



    
    

Sms.java

public class Sms {
    private int id;
    private String address;
    private String body;

    public Sms(int id, String address, String body) {
        this.id = id;
        this.address = address;
        this.body = body;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TextView tv_show;
    private Button bt_show;
    private List list=new ArrayList<>();
    private String text="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show=findViewById(R.id.tv_show);
        bt_show=findViewById(R.id.bt_show);
    }

    public void show(View view) {
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_SMS},1);
        }
        public void getSms(){
             Uri uri= Uri.parse("content://sms/");
            ContentResolver contentResolver=getContentResolver();
            Cursor cursor = contentResolver.query(uri, new String[]{"_id", "address",
                    "body"}, null, null, null);
            if (cursor!=null&&cursor.getCount()>0){
                if (list!=null){
                    list.clear();
                }
                text="";
                while (cursor.moveToNext()){
                    int id=cursor.getInt(0);
                    String name=cursor.getString(1);
                    String body=cursor.getString(2);
                    Sms sms=new Sms(id,name,body);
                    list.add(sms);
                }
                for (int i=0;i 
 

Android使用ContentProvider实现查看系统短信功能_第1张图片

Android使用ContentProvider实现查看系统短信功能_第2张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android使用ContentProvider实现查看系统短信功能)