3

如何以编程方式在 Android 中列出 sim 联系人?我在这里获得了获取电话联系人的代码,但我也需要 SIM 卡联系人。

4

4 回答 4

6
Uri simUri = Uri.parse("content://icc/adn");

  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")));
            }

现在只需按照代码获取 simcard 详细信息..它工作正常

于 2012-12-07T04:48:59.717 回答
4

试试这个代码伙伴!

     import android.app.Activity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;

    public class GetContactsDemo extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            readContacts();
        }

        public void readContacts(){
             ContentResolver cr = getContentResolver();
             Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);

             if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer.parseInt(cur.getString(cur.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);
                        }
                        pCur.close();


                        // get email and type

                       Cursor emailCur = cr.query(
                                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                new String[]{id}, null);
                        while (emailCur.moveToNext()) {
                            // This would allow you get several email addresses
                                // if the email addresses were stored in an array
                            String email = emailCur.getString(
                                          emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            String emailType = emailCur.getString(
                                          emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                          System.out.println("Email " + email + " Email Type : " + emailType);
                        }
                        emailCur.close();

                        // Get note.......
                        String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                        String[] noteWhereParams = new String[]{id,
                        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
                                Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
                        if (noteCur.moveToFirst()) {
                            String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                          System.out.println("Note " + note);
                        }
                        noteCur.close();

                        //Get Postal Address....

                        String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                        String[] addrWhereParams = new String[]{id,
                            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
                        Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
                                    null, null, null, null);
                        while(addrCur.moveToNext()) {
                            String poBox = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                            String street = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                            String city = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                            String state = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                            String postalCode = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.<span class="IL_AD" id="IL_AD3">POSTCODE</span>));
                            String country = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                            String type = addrCur.getString(
                                         addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

                            // Do something with these....

                        }
                        addrCur.close();

                        // Get Instant <span class="IL_AD" id="IL_AD12">Messenger</span>.........
                        String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                        String[] imWhereParams = new String[]{id,
                            ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
                        Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
                                null, imWhere, imWhereParams, null);
                        if (imCur.moveToFirst()) {
                            String imName = imCur.getString(
                                     imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                            String imType;
                            imType = imCur.getString(
                                     imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
                        }
                        imCur.close();

                        // Get Organizations.........

                        String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                        String[] orgWhereParams = new String[]{id,
                            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
                        Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
                                    null, orgWhere, orgWhereParams, null);
                        if (orgCur.moveToFirst()) {
                            String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
                            String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
                        }
                        orgCur.close();
                    }
                }
           }
        }
     }
于 2012-12-07T05:44:20.427 回答
0

尝试这个::

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 {

    ArrayList<String>listName=new ArrayList<String>();
    ArrayList<String>listContactId=new ArrayList<String>();
    ArrayList<String>listMobileNo=new ArrayList<String>();
    ArrayList<String>idArr=new ArrayList<String>();
    ArrayList<String>nameArr=new ArrayList<String>();

    @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);
    }
}

将此代码添加到您的 mainifest 文件中:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
于 2015-07-21T13:19:13.577 回答
-3

你试过这个

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();

还要在清单文件中包含 READ_PHONE_STATE 权限。

于 2012-12-07T04:48:14.080 回答