android利用ContentResolver访问者获取手机短信信息
程序员文章站
2023-12-19 12:24:22
利用contentresolver访问者获取手机短信信息,在此记录一下,一遍以后查询。
首先看一下结果,结果如下:
activity_message.xml类:...
利用contentresolver访问者获取手机短信信息,在此记录一下,一遍以后查询。
首先看一下结果,结果如下:
activity_message.xml类:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_message" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.messageactivity"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_message" > </listview> </linearlayout>
activity_xs.xml类
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_xs" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.xsactivity"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_name" /> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_telephone" /> </linearlayout>
messageactivity类:
public class messageactivity extends appcompatactivity { private listview lv_message; private contentresolver cr; private arraylist<map<string, object>> datalistview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_message); //获得短信的id lv_message = (listview) findviewbyid(r.id.lv_message); //得到访问者contentresolver cr = getcontentresolver(); //定义一个接收短信的集合 datalistview = new arraylist<>(); uri uri = uri.parse("content://sms/"); cursor cursor = cr.query(uri, null, null, null, null); while (cursor.movetonext()) { string body = cursor.getstring(cursor.getcolumnindex("body")); int address = cursor.getint(cursor.getcolumnindex("address")); //将号码和短信内容放入map集合中 map<string, object> map = new hashmap<>(); map.put("images", address+""); map.put("titles", body); datalistview.add(map); } simpleadapter adapter = new simpleadapter(this, datalistview, r.layout.activity_xs, new string[]{"images", "titles"}, new int[]{r.id.tv_name, r.id.tv_telephone}); lv_message.setadapter(adapter); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。