亲宝软件园·资讯

展开

android 获取短信 android利用ContentResolver访问者获取手机短信信息

ly593988490 人气:0
想了解android利用ContentResolver访问者获取手机短信信息的相关内容吗,ly593988490在本文为您仔细讲解android 获取短信的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,获取短信,android,获取所有短信,android,获取短信信息,下面大家一起来学习吧。

利用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);
 }
}

加载全部内容

相关教程
猜你喜欢
用户评论