那么这一节就迎来激动人心的通讯录在手天下我有的章节
- CallLog.Calls._ID
- CallLog.Calls.CACHED_NAME
- CallLog.Calls.NUMBER
想要知道你亲爱的他(她)是被动接到还是主动拨出么,这里涉及到,呼叫类型
CallLog.Calls.TYPE
- 拨出 outgoing
- 拨入 incoming
- 未接 missed
本实例只实现读取id,人名,电话号码,有心人可以自加功能
主布局函数:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/callList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
listview模板布局函数定义:
<?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">
<TableRow>
<TextView
android:id="@+id/_id"
android:textSize="20px"
android:layout_height="wrap_content"
android:layout_width="30px" />
<TextView
android:id="@+id/name"
android:textSize="20px"
android:layout_height="wrap_content"
android:layout_width="180px" />
<TextView
android:id="@+id/number"
android:textSize="20px"
android:layout_height="wrap_content"
android:layout_width="180px" />
</TableRow>
</TableLayout>
主函数Activity
public class MainActivity extends Activity {
private ListView calllist=null;
private Cursor result=null;//找联系人
private List<Map<String,Object>> allcalls=null;
private SimpleAdapter simple=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.calllist=(ListView)super.findViewById(R.id.callList);
//操作的uri,简单来说就是找到人家的门牌号。
this.result=super.getContentResolver().query(CallLog.Calls.CONTENT_URI,null, null, null, null);
this.startManagingCursor(result);//cursor交给系统管理
allcalls=new ArrayList<Map<String,Object>>();
//找到数据一个个往Map存
for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", result.getInt(result.getColumnIndex(CallLog.Calls._ID)));
String name=result.getString(result.getColumnIndex(CallLog.Calls.CACHED_NAME));
//设置未知电话,也就是手机联系人里所没有的
if(name==null||"".equals(name)){
name="未知";
}
map.put("name", name);
map.put("number", result.getString(result.getColumnIndex(CallLog.Calls.NUMBER)));
this.allcalls.add(map);
}
this.simple=new SimpleAdapter(this,this.allcalls,
R.layout.call,//模板
new String[]{"id","name","number"},//匹配Map集合的key
new int[]{R.id._id,R.id.name,R.id.number}//设置显示数据
);
this.calllist.setAdapter(simple);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
注意配置权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
效果如下: