在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0066ff">
<ListView
android:id="@+id/callList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
新建布局文件calls.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0066ff">
<TableRow >
<TextView
android:id="@+id/_id"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/name"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="8dp"/>
<TextView
android:id="@+id/number"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="8dp"/>
</TableRow>
</TableLayout>
在主Activity(MyCallContentProviderDmeo.java)中:
package com.li.callcontentprovider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.provider.CallLog;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;
public class MyCallContentProviderDmeo extends Activity {
private ListView callList = null;
private Cursor result = null;
private List<Map<String,Object>> allCalls = null;
private SimpleAdapter simple = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.callList = (ListView)super.findViewById(R.id.callList);
this.result = super.getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, null); //查询通话记录
this.startManagingCursor(this.result); //交给程序
this.allCalls = new ArrayList<Map<String,Object>>();
for(this.result.moveToFirst();!result.isAfterLast();
result.moveToNext()){ //取出数据
Map<String,Object> contact = new HashMap<String, Object>();
contact.put("_id",result.getInt(result
.getColumnIndex(CallLog.Calls._ID)));
String nameTemp = this.result.getString(this.result.getColumnIndex(
CallLog.Calls.CACHED_NAME));
if(nameTemp == null || "".equals(nameTemp)){ //如果是未知号码
nameTemp = "未知号码";
}
contact.put("name",nameTemp);
contact.put("number",result.getString(result
.getColumnIndex(CallLog.Calls.NUMBER)));
this.allCalls.add(contact);
}
this.simple = new SimpleAdapter(this,this.allCalls,
R.layout.calls,new String[]{"_id","name","number"},
new int[]{R.id._id,R.id.name,R.id.number});
this.callList.setAdapter(this.simple);
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.callcontentprovider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyCallContentProviderDmeo"
android:label="@string/title_activity_my_call_content_provider_dmeo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
</manifest>