How to get All Sim Contacts in Android programmatically?

前端 未结 4 735
失恋的感觉
失恋的感觉 2021-01-20 19:28

How to list sim contacts in Android programmatically? I got the code to get phone contacts here but I need sim contacts too with this.

4条回答
  •  天涯浪人
    2021-01-20 20:01

    Try this::

    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class MainActivity extends Activity {
    
        ArrayListlistName=new ArrayList();
        ArrayListlistContactId=new ArrayList();
        ArrayListlistMobileNo=new ArrayList();
        ArrayListidArr=new ArrayList();
        ArrayListnameArr=new ArrayList();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Uri simUri = Uri.parse("content://icc/adn");
    
            ContentResolver cr = getContentResolver();
            Cursor cursorSim = cr.query(ContactsContract.Contacts.CONTENT_URI,
                   null, null, null, null);
           // Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null);
    
                   while (cursorSim.moveToNext()) {           
                    //   listName.add(cursorSim.getString(cursorSim.getColumnIndex("name")));
                     //  listContactId.add(cursorSim.getString(cursorSim.getColumnIndex("_id")));      
                     //  listMobileNo.add(cursorSim.getString(cursorSim.getColumnIndex("number")));
                       String id = cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts._ID));
                       String name = cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    
    
                     //  String number=cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                       idArr.add(id);
                       nameArr.add(name);
    
                     //  listMobileNo.add(number);
    
    
                       if (Integer.parseInt(cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    
                       System.out.println("name : " + name + ", ID : " + id);
    
                       // get the phone number
                       Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                              new String[]{id}, null);
                       while (pCur.moveToNext()) {
                             String phone = pCur.getString(
                                    pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                             System.out.println("phone=" + phone);
                             listMobileNo.add(phone);
                       }
                       pCur.close();
    
                      }
                   }
                   System.out.println("idArr="+idArr);
                   System.out.println("nameArr="+nameArr);
    
                 //  System.out.println("listName="+listName);
                  // System.out.println("listContactId="+listContactId);
                   System.out.println("listMobileNo="+listMobileNo);
    
        }
    
    
        @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;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    

    Add this code to your mainifest file:

    
    

提交回复
热议问题