找了好久终于可以读取SIM通讯录了
美中不足的是 读取到的数据 也包含了 手机通讯录的数据
不管怎么样 总算是 读到了数据
再把手机通讯录的数据过滤掉就好了
类 Sim
package com.contact.phone;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
@SuppressWarnings("unused")
public class Sim extends ListActivity {
private String tag = "contact";
private List<String> nameList = new ArrayList<String>();
private List<String> mobileList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_list);
listSimMetadata();
listSimContact();
setListAdapter(new MyAdapter(Sim.this,nameList,mobileList));
}
private void listSimContact(){
Uri uri = Uri.parse("content://icc/adn");
Cursor contacts = managedQuery(uri , null , null , null , null);
Log.d("contact", contacts.getCount()+"");
while(contacts.moveToNext()){
String name = contacts.getString(contacts.getColumnIndex("name"));
String number = contacts.getString(contacts.getColumnIndex("number"));
Log.d("contact" , name + " = " + number);
nameList.add(name);
mobileList.add(number);
}
contacts.close();
}
/**
* 会把Sim卡和通讯录的数据全部读取出来
* 重叠返回
* 经测试 SIM卡内共有 _id,name,number,emails 四列数值
* 需要权限
* android.permission.READ_CONTACTS
* android.permission.WRITE_CONTACTS
* android.permission.READ_PHONE_STATE
*/
private void listSimMetadata(){
// Uri uri = Uri.parse("content://sim/adn");
Uri uri = Uri.parse("content://icc/adn");
Cursor contacts = managedQuery(uri , null , null , null , null);
Log.d("contact", contacts.getCount()+"");
while(contacts.moveToNext()){
String [] columns = contacts.getColumnNames();
for(int i = 0 ; i < columns.length ; i++){
String key = contacts.getColumnName(i);
String value = contacts.getString(contacts.getColumnIndex(key));
Log.d("contact" , key + " = " + value);
}
Log.d("contact", "==========================================================");
}
contacts.close();
}
}
类 MyAdapter
package com.sgf.contact.phone;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<String> name;
private List<String> mobile;
public MyAdapter(Context context, List<String> name, List<String> mobile) {
layoutInflater = LayoutInflater.from(context);
this.name = name;
this.mobile = mobile;
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
return name.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup par) {
ViewHolder holder = null ;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.mobile = (TextView) convertView.findViewById(R.id.mobile);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(name.get(position).toString());
holder.mobile.setText(mobile.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView name;
TextView mobile;
}
}
布局文件 row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/name"
android:layout_width="100dip"
android:layout_height="40px"
android:layout_weight="1.0"
android:textSize="18sp"
android:textColor="#FFFF00"
android:layout_gravity="right"
/>
<TextView android:id="@+id/mobile"
android:layout_width="wrap_content"
android:layout_height="40px"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
android:textSize="16sp"
android:textColor="#008000"
/>
</LinearLayout>
布局文件 phone_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
></ListView>
</LinearLayout>