这里给大家带来的是短信的数据读取,联系人、手机号码、手机内容、信息时间、类型
下面是用listView展示到自己的app中
效果图:
代码如下:
activity_sms.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_sms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android25_resolver.SMSActivity">
<ListView
android:layout_width="match_parent"
android:id="@+id/ls_sms"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
layout_listview_sms.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tv_item_list_name"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tv_item_list_phone"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tv_item_list_body"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tv_item_list_date"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/tv_item_list_type"
/>
</LinearLayout>
SMSActivity.java:
package com.example.android25_resolver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static android.R.attr.phoneNumber;
import static android.R.id.list;
import static com.example.android25_resolver.R.id.ls_contacts;
public class SMSActivity extends AppCompatActivity {
final String SMS_URI_ALL = "content://sms/";//全部
final String SMS_URI_INBOX = "content://sms/inbox";//收件
final String SMS_URI_SEND = "content://sms/sent";//发送
final String SMS_URI_DRAFT = "content://sms/draft";//草稿箱
private ListView ls_sms;
private ContentResolver cr;
private SimpleAdapter simpleAdapter;
private List<Map<String, String>> sms = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
//得到内容访问者
cr = getContentResolver();
//得到listview
ls_sms = (ListView) findViewById(R.id.ls_sms);
//实例一个简易适配器
simpleAdapter = new SimpleAdapter(this, sms, R.layout.layout_listview_sms, new String[]{"name", "phone", "body", "date", "type"}
, new int[]{R.id.tv_item_list_name, R.id.tv_item_list_phone, R.id.tv_item_list_body,
R.id.tv_item_list_date, R.id.tv_item_list_type});
//给listView设置适配器
ls_sms.setAdapter(simpleAdapter);
getSms();//初始化数据
}
public void getSms() {
Uri uri = android.net.Uri.parse(SMS_URI_ALL);
Cursor cursor = cr.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
Map<String, String> String = new HashMap<>();
int nameColumn = cursor.getColumnIndex("person");// 联系人姓名列表序号
int phoneNumberColumn = cursor.getColumnIndex("address");// 手机号
int smsbodyColumn = cursor.getColumnIndex("body");// 短信内容
int dateColumn = cursor.getColumnIndex("date");// 日期
int typeColumn = cursor.getColumnIndex("type");// 收发类型 1表示接受 2表示发送
String nameId = cursor.getString(nameColumn);
String phoneNumber = cursor.getString(phoneNumberColumn);
String smsbody = cursor.getString(smsbodyColumn);
Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd " + "\n" + "hh:mm:ss");
String date = dateFormat.format(d);
String type = cursor.getString(typeColumn);
Uri personUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, phoneNumber);
Cursor localCursor = cr.query(personUri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.PHOTO_ID, ContactsContract.PhoneLookup._ID}, null, null, null);
// if (localCursor.getCount() != 0) {
// localCursor.moveToFirst();
// System.out.println("之后----" + localCursor);
// String name = localCursor.getString(localCursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
// }
String.put("name", nameId);
String.put("phone", phoneNumber);
String.put("body", smsbody);
String.put("date", date);
if ("1".equals(type)) {
type = "收件";
} else {
type = "发件";
}
String.put("type", type);
sms.add(String);
}
simpleAdapter.notifyDataSetChanged();//通知适配器发生改变
}
}
最后加上权限
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>