Android使用ContentProvider实现查看系统短信功能
作者:星辞归也 时间:2023-12-16 23:55:46
本文实例为大家分享了使用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();
}
}
}
}
}
来源:https://blog.csdn.net/weixin_43916678/article/details/119428728
标签:Android,系统短信
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
VScode 打造完美java开发环境最新教程
2023-02-24 16:02:10
代码分析Android实现侧滑菜单
2022-04-02 16:38:45
![](https://img.aspxhome.com/file/2023/1/111671_0s.gif)
Android Studio 通过登录功能介绍SQLite数据库的使用流程
2023-09-22 00:37:29
SpringBoot详解如何进行整合Druid数据源
2022-05-10 10:46:54
![](https://img.aspxhome.com/file/2023/0/106080_0s.png)
JavaScript嵌入百度地图API的最详细方法
2023-04-12 14:33:03
![](https://img.aspxhome.com/file/2023/4/133604_0s.jpg)
深入理解Android Bitmap
2023-07-29 08:52:14
java源码解析之String类的compareTo(String otherString)方法
2023-11-11 23:10:00
C#使用HttpClient的正确方式你了解吗
2023-07-04 12:12:40
![](https://img.aspxhome.com/file/2023/1/129721_0s.png)
java高并发ScheduledThreadPoolExecutor与Timer区别
2023-08-11 03:08:29
解决FeignClient重试机制造成的接口幂等性
2022-01-25 11:10:35
C#在图片增加文字的实现代码
2023-03-30 03:26:24
Android开发解决popupWindow重叠报错问题
2022-08-02 13:27:01
Android开发实现控件双击事件的监听接口封装类
2023-02-15 00:56:18
C#下listview如何插入图片
2023-06-18 11:45:44
maven下载jar包改用阿里云maven库的方法
2023-02-13 07:41:58
C# 无边框窗体边框阴影效果的简单实现
2021-12-06 23:41:43
使用C#获取远程图片 Form用户名与密码Authorization认证的实现
2022-01-22 21:44:06
Spring在多线程下@Resource注入为null的问题
2022-09-11 07:41:18
Android Studio 3.0的下载安装教程
2021-08-10 04:00:45
![](https://img.aspxhome.com/file/2023/6/93306_0s.png)
Android 使用CoordinatorLayout实现滚动标题栏效果的实例
2021-11-16 07:47:09
![](https://img.aspxhome.com/file/2023/8/102578_0s.gif)