휴대전화 통신록 정보 획득 예시

13960 단어
매우 간단한 데모 예제에서 Java 코드는 다음과 같습니다.
public class MainActivity extends Activity {
   
   private String uri_raw = "content://com.android.contacts/raw_contacts";
   private String uri_phone = "content://com.android.contacts/data/phones";   
   private ListView listView  = null;
   private TextView empty    = null;
   private ContentResolver contentResolver; 
   private List, String>> data = new ArrayList,String>>();
   private SimpleAdapter adapter;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      listView= (ListView) findViewById(R.id.lv);
      empty = (TextView) findViewById(R.id.empty);
      listView.setEmptyView(empty);  
      adapter = new SimpleAdapter(this, data, R.layout.item, 
            new String[]{"_id","display_Name","data1"}, new int []{R.id.id,R.id.name,R.id.number});    
      listView.setAdapter(adapter);    
      contentResolver = getContentResolver(); //    v4 
      reloadListView();
   }
   private void reloadListView() {
      // TODO Auto-generated method stub
      data.clear(); // 
      data.addAll(selectData()); 
      adapter.notifyDataSetChanged();
   }
   //    Data  raw_contact   List   Adapter
   private List, String>> selectData() {
      //1.  raw  _id  .
      //2. -->  _id  data (_id = raw_contacts_id)  data1 
      Cursor cursor = contentResolver.query(Uri.parse(uri_raw), 
                   new String []{"_id","display_name"}, null, null, null);
      //cursor cursor 
      List, String>>  list =  new ArrayList,String>>();     
      while (cursor!= null && cursor.moveToNext()) {       
         String _id = cursor.getString(0);
         String dispalyName  = cursor.getString(1);
         Map, String> map = new HashMap, String>();
         map.put("_id", _id);
         map.put("display_Name", dispalyName);        
         // _id  data 
         Cursor cursor2 = contentResolver.query(Uri.parse(uri_phone), new String[]{"data1"}, 
               "raw_contact_id=?", new String []{_id}, null);
         // 
         StringBuffer sb = new StringBuffer();
         while (cursor2!=null && cursor2.moveToNext()) {           
            sb.append(cursor2.getString(0)+"
"); } cursor2.close(); // map.put("data1", sb.toString()); list.add(map); } cursor.close(); return list; } }

좋은 웹페이지 즐겨찾기