android利用ContentResolver访问者获取手机短信信息
更新时间:2017年02月22日 08:43:10 作者:ly593988490
本篇文章主要介绍了android利用ContentResolver访问者获取手机短信信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
利用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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android 中的 Timer 和 TimerTask详解
Timer 是 Java 中用于创建定时任务的类,它位于 java.util 包中,可以使用Timer来安排一次性或定期执行的任务,这篇文章主要介绍了Android 的 Timer 和 TimerTask,需要的朋友可以参考下2024-05-05
Android编程开发实现TextView显示表情图像和文字的方法
这篇文章主要介绍了Android编程开发实现TextView显示表情图像和文字的方法,结合实例形式分析了Android中TextView的使用技巧,需要的朋友可以参考下2015-12-12
Android如何使用RecyclerView打造首页轮播图
这篇文章主要为大家详细介绍了Android如何使用RecyclerView打造首页轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-02-02
Android的Launcher启动器中添加快捷方式及小部件实例
这篇文章主要介绍了在Android的Launcher启动器中添加快捷方式及窗口小部件的方法,包括在自己的应用程序中添加窗口小部件AppWidget的例子,需要的朋友可以参考下2016-02-02
Android中封装RecyclerView实现添加头部和底部示例代码
这篇文章主要给大家介绍了关于Android中封装RecyclerView实现添加头部和底部的相关资料,网上这方面的资料很多,但都不是自己需要的,索性自己写一个分享出来供大家参考学习,需要的朋友们下面随着小编一起来学习学习吧。2017-08-08
Android ListView优化之提高android应用效率
android listview优化做的好是提高androoid应用效率的前提条件,本文给大家介绍Android ListView优化之提高android应用效率,对android listview优化相关知识感兴趣的朋友一起学习吧2015-12-12


最新评论