转载自:http://android.tgbus.com/Android/androidnews/200902/178074.shtml

Android平台开放程度的确很厉害,你几乎可以调用任何底层的接口,甚至拦截到短信或者呼入电话。这些是J2ME平台无法比拟的。本文介绍一下如何访问android的通话记录。

  android平台上的通话记录是以Content Provider的形式存储在手机上的,因此你需要使用ContentResolver来查询通话记录,返回Cursor接口。如下所示:

package com.me;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

public class CallLogActivity extends ListActivity {

@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.main);
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] { CallLog.Calls.NUMBER },
new int[] { android.R.id.text1 });

setListAdapter(adapter);
}

}

  获得了Cursor之后便可以构建一个Adapter然后调用setListAdapter()来把通话记录显示在屏幕上。CallLog类中定义了Calls类,在android中可以看到大量的内部类的设计。Calls定义了很多常量,方便你来访问通话记录,主要包括两个URI和多个字段定义,比如我们在这里用到的NUMBER。更多内容请参考Andorid doc。

  下面是/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Notes!"/>
</LinearLayout>

更多相关文章

  1. Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
  2. 自定义控件及效果
  3. 第三部分:Android 应用程序接口指南---第一节:应用程序组件---第二
  4. Android菜鸟的成长笔记(6)——剖析源码学自定义主题Theme
  5. Android与iOS自定义URL Scheme唤醒app
  6. 为android开放类增加自定义成员方法
  7. Android如何开发自定义编译时注解

随机推荐

  1. 2021-02-28:给定一个整型数组arr,和一个整
  2. MySQL的防火墙功能
  3. MySQL监视工具MEM
  4. MySQL企业版备份工具MEB
  5. 第三周
  6. MGR用哪个版本?5.7 vs 8.0
  7. MySQL字段默认值设置详解
  8. 我的第32个代码
  9. CentOS8配置本地yum
  10. 广州市翊腾通讯设备有限公司