`

访问android平台的通话记录CallLog

阅读更多
转载自: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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics