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 删除指定包名的App实例代码

    Android 删除指定包名的App实例代码

    这篇文章主要介绍了Android 删除指定包名的App实例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-07-07
  • 在Android里完美实现基站和WIFI定位

    在Android里完美实现基站和WIFI定位

    众所周知的,在OPhone和大部分国产的Android定制机里不支持最简单实用的基站和WIFI定位,只能使用速度慢而耗电的GPS定位,但OPhone和华为/中兴生产的一些Android定制机却占据了一定的市场,因此导致了很多使用了定位技术的Andorid应用挺尴尬的。
    2014-07-07
  • 使用Android studio编写一个小的jni程序

    使用Android studio编写一个小的jni程序

    JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++)。这篇文章给大家介绍了基于Android studio写一个小的jni程序的方法,一起看看吧
    2018-03-03
  • Android布局优化之ViewStub控件

    Android布局优化之ViewStub控件

    ViewStub是一个非常轻量级的View,这篇文章主要为大家详细介绍了Android布局优化之ViewStub控件的使用方法以及注意事项,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 清楚详解Android 进程间图传递图形buffer原理

    清楚详解Android 进程间图传递图形buffer原理

    这篇文章主要为大家清楚的详解了Android 进程间图传递图形buffer原理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android编程向服务器发送请求时出现中文乱码问题的解决方法

    Android编程向服务器发送请求时出现中文乱码问题的解决方法

    这篇文章主要介绍了Android编程向服务器发送请求时出现中文乱码问题的解决方法,实例分析了Android参数传递过程中中文乱码的解决技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • android桌面悬浮窗显示录屏时间控制效果

    android桌面悬浮窗显示录屏时间控制效果

    这篇文章主要为大家详细介绍了android桌面悬浮窗,显示录屏时间控制效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • android加密参数定位实现方法

    android加密参数定位实现方法

    这篇文章主要介绍了android加密参数定位方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Android miniTwitter登录界面开发实例

    Android miniTwitter登录界面开发实例

    这篇文章主要为大家详细介绍了Android miniTwitter登录界面开发实例,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • android如何改变editText控件中部分文字的格式

    android如何改变editText控件中部分文字的格式

    本文主要介绍了android改变editText控件中部分文字格式的方法,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03

最新评论