Android使用系统的Content Provider

原创
2015/05/28 10:44
阅读数 16
Content Provider的Uri的格式为:

  content://<authority>/<data_path>/id
例子:
content://media/internal/images   图片
content://contacts/people              联系人
content://browser/bookmarks       浏览器书签


public class MainActivity extends ListActivity {
 
 Cursor Name_cursor;
 //Contact_data[] contact_datas;
 List<Contact_data> contact_datas = new ArrayList<>();
 //Cursor Phone_cursor;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // setContentView(R.layout.contact_layout);
 //setContentView(R.layout.activity_main);
 
 //Uri allContacts = Uri.parse("content://contacts/people"); 与下一行效果一样
 Uri allContacts = ContactsContract.Contacts.CONTENT_URI;
 String[] projection = new String[]{
 ContactsContract.Contacts._ID,
 ContactsContract.Contacts.DISPLAY_NAME,
 ContactsContract.Contacts.HAS_PHONE_NUMBER
 };
 if (Build.VERSION.SDK_INT < 11) {
 Name_cursor = managedQuery(allContacts, null, null, null, null);
 } else {
 CursorLoader cursorLoader = new CursorLoader(this, allContacts, projection, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", new String[]{"%Li%"}, ContactsContract.Contacts.DISPLAY_NAME + " DESC");
 Name_cursor = cursorLoader.loadInBackground();
 }
 
 Name_cursor.moveToFirst();
 
 // Toast.makeText(this, "Name cursor: " + Name_cursor.getCount() + "\n" + Name_cursor.getString(Name_cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + "\n" + Name_cursor.getString(Name_cursor.getColumnIndex(ContactsContract.Contacts._ID)), Toast.LENGTH_LONG).show();
 /* 方法一: 使用 SimpleCursorAdapter ,有局限性
 String[] columns = new String[]{
 ContactsContract.Contacts.DISPLAY_NAME,
 ContactsContract.Contacts._ID
 };
 
 int[] views = new int[]{R.id.contactName, R.id.contactID};
 
 SimpleCursorAdapter simpleCursorAdapter;
 
 simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.activity_main, Name_cursor, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
 
 this.setListAdapter(simpleCursorAdapter);
 
 方法二:使用自定义的Adapter
 */
 MyContactAdapter myContactAdapter = new MyContactAdapter(this);
 
 PrintContacts(Name_cursor);
 
 this.setListAdapter(myContactAdapter);
 
 }
 
 
 private void PrintContacts(Cursor cursor) {
 if (cursor.moveToFirst()) {
 do {
 
 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
 String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
 
 Log.v("Content Provider", contactId + "," + contactName);
 
 int hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
 if (hasPhone == 1) {
 Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
 String contactPhone = "";
 while (cursorPhone.moveToNext()) {
 contactPhone = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 Log.v("Content Provider", contactPhone);
 }
 cursorPhone.close();
 Contact_data contact_data = new Contact_data(contactName, contactPhone, contactId);
 
 contact_datas.add(contact_data);
 }
 } while (cursor.moveToNext());
 }
 }
 
 public class MyContactAdapter extends BaseAdapter {
 
 Context context;
 
 public MyContactAdapter(Context c) {
 context = c;
 }
 
 @Override
 public int getCount() {
 return contact_datas.size();
 }
 
 @Override
 public Object getItem(int position) {
 return contact_datas.get(position);
 }
 
 @Override
 public long getItemId(int position) {
 return position;
 }
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 LinearLayout linearLayout = null;
 if (convertView == null) {
 linearLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contact_layout, null);
 } else {
 linearLayout = (LinearLayout) convertView;
 }
 
 Contact_data mydata = (Contact_data) this.getItem(position);
 String contact_id = mydata.id;
 String contact_name = mydata.name;
 String contact_phone = mydata.phone;
 
 TextView id_text = (TextView) linearLayout.findViewById(R.id.mycontactId);
 id_text.setText(contact_id);
 TextView name_text = (TextView) linearLayout.findViewById(R.id.mycontactName);
 name_text.setText(contact_name);
 TextView phone_text = (TextView) linearLayout.findViewById(R.id.mycontactPhone);
 phone_text.setText(contact_phone);
 return linearLayout;
 }
 }
 
}




public class Contact_data {
 String name;
 String phone;
 String id;
 
 public Contact_data(String name, String phone, String id){
 this.id = id;
 this.name = name;
 this.phone = phone;
 }
}




主要用到的类: Cursor List < Contact_data > Uri   CursorLoader
主要用到的方法: CursorLoader()构造方法 loadInBackground () moveToFirst () setListAdapter() getString() getColumnIndex() getContentResolver () query()

关键:  使用  CursorLoader()构造方法 和  loadInBackground () 查询内容,返回Cursor对象 (在后台线程中执行cursor的查询,不会阻塞用户界面);
  在查询phone number中,使用了 getContentResolver (). query(...). (联系人的名字等信息和电话号码存放在2个不同的tables里,所以需要2次查询);
  在Cursor类中检索内容,必须已知内容的type进而使用对应的getXxx()方法(如 getString(),getInt()),参数为columnIndex 可用 getColumnIndex() 进行查询。







展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部