Android获取Contact Number的例子(2.0系统以前的获取方式)

来源:互联网 发布:mac onenote 文字识别 编辑:程序博客网 时间:2024/06/06 21:49
  1. http://www.blogjava.net/caizh2009/archive/2010/12/17/341010.html
  2. String[] projection = new String[] {  
  3. People._ID,  
  4. People.NAME,  
  5. People.NUMBER,  
  6. };  
  7.   
  8. // Get the base URI for People table in Contacts content provider.  
  9. // which is: content://contacts/people/  
  10. Uri contactUri = People.CONTENT_URI;  
  11.   
  12. // Best way to retrieve a query; returns a managed query.  
  13. Cursor peopleCursor = managedQuery (contactUri,  
  14. projection, //Which columns to return.  
  15. null, // WHERE clause--we won't specify.  
  16. null, // Selection Args??  
  17. People.DEFAULT_SORT_ORDER); // Order-by name  
  18.   
  19. // go to the beginning of the list  
  20. peopleCursor.moveToFirst();  
  21.   
  22.   
  23. // So, here we have a contact. We need to get the contact ID (_id) then  
  24. // build the Uri to get the phones section of that user's record  
  25. // which is a subdirectory of a contact record  
  26.   
  27. long personId = peopleCursor.getLong(peopleCursor.getColumnIndex("_id"));  
  28.   
  29. Uri personUri = ContentUris.withAppendedId(contactUri, personId );  
  30.   
  31. // So now the URL looks like: content://contacts/people/_id(where the actual id of the record is here)  
  32. Uri phoneUri=  
  33. Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY);  
  34.   
  35. // Now the URL looks like: content://contacts/people/_id/phones (where phones is literally "phones")  
  36.   
  37. // Now get all the phone numbers for this contact  
  38. Cursor phonesCursor = managedQuery(phoneUri,  
  39. null,  
  40. null,  
  41. null,  
  42. Phones.DEFAULT_SORT_ORDER);  
  43.   
  44. // We now have a cursor for all the phone numbers for that User ID  
  45. // go to the beginning of the phone list.  
  46. phonesCursor.moveToFirst();   
0 0