所需权限
<!-- 获取拨打电话的记录权限 --> <uses-permission android:name="android.permission.READ_CALL_LOG"/>
layout 布局文件
mainActivity<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂无数据" android:layout_centerInParent="true" android:textColor="#f00" android:textSize="30sp" /> </RelativeLayout>
list填充文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
逻辑代码 mainActivity.java
package com.qf.day16_contentresolver_call_demo2; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.SimpleFormatter; import android.net.Uri; import android.os.Bundle; import android.provider.CallLog; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; /** * * 通话记录的数据 * * 1,获取ContentResolver对象 2,ContentResolver对象 通过Uri地址 获取数据 3,展示数据 * */ public class MainActivity extends Activity { private ListView lv; private TextView tv; private ContentResolver contentResolver; //content://call_log/calls private Uri callUri = CallLog.Calls.CONTENT_URI; private String [] coumns = new String[]{CallLog.Calls._ID,CallLog.Calls.NUMBER,CallLog.Calls.DATE}; //存数据源的集合 private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); tv = (TextView) findViewById(R.id.tv); contentResolver = getContentResolver(); //解析器 通过Uri地址获取指定的字段项 Cursor cursor = contentResolver.query(callUri, coumns, null, null, null); while(cursor.moveToNext()){ Map<String, Object> map = new HashMap<String, Object>(); long id = cursor.getLong(cursor.getColumnIndex(CallLog.Calls._ID)); String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)); map.put("id", id); map.put("number", number); map.put("date", format.format(date)); list.add(map); } SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.item, new String[]{"number","date"}, new int[]{R.id.tv_number,R.id.tv_date}); lv.setAdapter(adapter); //如果没有数据 展示VIew lv.setEmptyView(tv); } }