欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2022-05-03 10:57:08|栏目:Android代码|点击:

本文实例为大家分享了使用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中FloatingActionButton实现悬浮按钮实例

栏    目:Android代码

下一篇:Android ArrayMap源代码分析

本文标题:Android使用ContentProvider实现查看系统短信功能

本文地址:http://www.codeinn.net/misctech/200881.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有