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

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

程序员文章站 2022-03-09 18:15:50
本文实例为大家分享了使用contentprovider实现查看系统短信功能的具体代码,供大家参考,具体内容如下activity_main.xml

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".mainactivity">
    <textview
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textsize="20dp"
        android:layout_weight="9"></textview>
    <button
        android:id="@+id/bt_show"
        android:layout_weight="1"
        android:text="查看短信"
        android:onclick="show"
        android:layout_width="match_parent"
        android:layout_height="0dp"></button>
</linearlayout>

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<sms> 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<list.size();i++){
                    text+="手机号码:"+list.get(i).getaddress()+"\n";
                    text+="短信内容:"+list.get(i).getbody()+"\n\n";
                }
                tv_show.settext(text);
            }
        }

    @override
    public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {
        super.onrequestpermissionsresult(requestcode, permissions, grantresults);
        if (requestcode==1){
            for (int i=0;i<permissions.length;i++){
                if (grantresults[i]== packagemanager.permission_granted){
                    getsms();
                }else {
                    toast.maketext(mainactivity.this,"无权限",toast.length_short).show();
                }
            }
        }
    }


}

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

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

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