如何从电话簿及放大器的所有联系人;在Android的SIM卡?电话簿、放大器、联系人、SIM

2023-09-05 08:50:07 作者:╰茉莉花

我的工作得到所有的电话本和SIM卡联系人在我的应用程序。我想这些存储在我的应用程序的SQLite数据库的所有联系人。在code与我的工作是在正常条件下工作的罚款。获取问题如下条件:

有没有名字的接触,这是唯一的编号。 联系人从SIM卡。

这些2类型的联系人,都没有提供给我的我的code。我现在用的是跌破code:

 公共无效getDefaultContactsToDB(){
        CallBackDatabase callbackDB =新CallBackDatabase(RequestCallBack.this);
        callbackDB.open();

        进入新的值之前//清理数据库。
        callbackDB.deleteTable(CallBackDatabase.DATABASE_TABLE);

        ContentResolver的CR = getContentResolver();
        光标CUR = cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
        如果(cur.getCount()大于0){
            而(cur.moveToNext()){
                字符串ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                光标CUR1 = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,空,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID +=?,
                        新的String [] {ID},NULL);
                而(cur1.moveToNext()){
                    //获取联系人姓名
                    ArrayList的<字符串>数=新的ArrayList<字符串>();
                    字符串contactNoumber =;
                    字符串名称= cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    如果(名字!= NULL){
                        Log.e(姓名:,姓名);
                        如果(name.equalsIgnoreCase(011999999999999999)){
                            的System.out.println(知道了);
                        }
                    }
                    //获取联系人电子邮件
                    字符串email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    如果(电子邮件!= NULL)
                        Log.e(电子邮件,电子邮件);

                    串hasNoumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    如果(的Integer.parseInt(hasNoumber)大于0){
                        光标pCur = getContentResolver()查询(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=
                                        + cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)),NULL,NULL);
                        INT I = 0;
                        而(pCur.moveToNext()){
                            //获取联系人号码
                            contactNoumber = pCur.getString(pCur.getColumnIndex(DATA1));
                            如果(contactNoumber.equalsIgnoreCase(011999999999999999)){
                                的System.out.println(知道了);
                            }
                            contactNoumber = Constant.removeSpecialCharacters(contactNoumber);
                            Log.e(contactNoumber,contactNoumber);
                            //获取电话号码
                            字符串numType = NULL;
                            if(pCur.getString(pCur.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){

                                开关(pCur.getInt(pCur.getColumnIndex(数据2))){
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                    numType =HOME;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                    numType =移动;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                    numType =工作;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                                    numType =其他;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT:
                                    numType =其他;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK:
                                    numType =回调;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_CAR:
                                    numType =CAR;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN:
                                    numType =公司主导;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
                                    numType =传真首页;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
                                    numType =传真工作;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
                                    numType =主;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_ISDN:
                                    numType =ISDN;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_MMS:
                                    numType =彩信;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX:
                                    numType =对方传真;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
                                    numType =寻呼机;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_RADIO:
                                    numType =RADIO;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_TELEX:
                                    numType =电传;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD:
                                    numType =TTY TDD;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
                                    numType =工作手机;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER:
                                    numType =单位寻呼机;
                                    打破;
                                案例ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
                                    numType =CUSTOM;
                                    打破;
                                默认:
                                    打破;
                                }
                            }

                            numbers.add(ⅰ,contactNoumber +(+ numType +));

                            我++;
                        }
                        字符串numInDB = NULL;
                        对于(INT J = 0; J<我; J ++){
                            如果(j == 0)
                                numInDB = numbers.get(J);
                            其他
                                numInDB = numInDB +,+ numbers.get(j)条;
                        }
                        如果(contactNoumber.length()大于0){
                            如果(名字!= NULL){

                            }其他{
                                NAME = contactNoumber;
                            }
                            callbackDB.InsertContacts(空,名+=+ numInDB,contactNoumber,电子邮件);
                        }
                    }
                }
                cur1.close();
            }
            //关闭数据库
            callbackDB.close();
        }
    }
 

解决方案

下面是code这显示了一个简单的方法来读取所有电话号码名称

 光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);
   而(phones.moveToNext())
   {
    字符串名称= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    串phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  }
   phones.close();
 
手机卡放在iphone里电话簿里怎么没号码

另外对于SIM卡联系人不仅可以使用以下code:

 私人无效allSIMContact()
  {
    尝试
    {
        字符串m_simPhonename = NULL;
        字符串m_simphoneNo = NULL;

        乌里simUri = Uri.parse(内容:// ICC / ADN);
        光标cursorSim = this.getContentResolver()查询(simUri,NULL,NULL,NULL,NULL);

        Log.i(PhoneContact,总:+ cursorSim.getCount());

        而(cursorSim.moveToNext())
        {
            m_simPhonename = cursorSim.getString(cursorSim.getColumnIndex(名字));
            m_simphoneNo = cursorSim.getString(cursorSim.getColumnIndex(数字));
            m_simphoneNo.replaceAll(\\ D,);
            m_simphoneNo.replaceAll(与&,);
            m_simPhonename = m_simPhonename.replace(|,);

            Log.i(PhoneContact,姓名:+ m_simPhonename +电话:+ m_simphoneNo);
        }
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }
}
 

编辑:

要得到这样的家庭,移动联系人的详细信息,传真等,则需要检查该单独如下:

 而(phone_crsr.moveToNext())
            {
       INT PHONE_TYPE = phone_crsr.getInt(phone_crsr.getColumnIndex(Phone.TYPE));
            开关(PHONE_TYPE)
            {
            案例Phone.TYPE_HOME:
                 phone_home = phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 Toast.makeText(这一点,家+ phone_home,Toast.LENGTH_LONG).show();
                 打破;
            案例Phone.TYPE_MOBILE:
                 phone_mob=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 Toast.makeText(这一点,暴民+ phone_mob,Toast.LENGTH_LONG).show();
                 打破;
            案例Phone.TYPE_WORK:
                 phone_work=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  Toast.makeText(这一点,工作+ phone_work,Toast.LENGTH_LONG).show();
                         打破;
             }
            }
 

I am working to get all the contacts from phone book and SIM card in my application. I want to store those all contacts in my application SQLite DB. The code with which I am working is working fine in normal conditions. Getting problem in below conditions:

with a contact without name, that is only number. Contacts from SIM card.

These 2 types of contacts, are not provided to me by my code. I am using the below code:

public void getDefaultContactsToDB(){
        CallBackDatabase callbackDB = new CallBackDatabase(RequestCallBack.this);
        callbackDB.open();

        //clean the database before entering new values.
        callbackDB.deleteTable(CallBackDatabase.DATABASE_TABLE);

        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));
                Cursor cur1 = cr.query( 
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                        new String[]{id}, null); 
                while (cur1.moveToNext()) { 
                    //to get the contact names
                    ArrayList<String> numbers= new ArrayList<String>();
                    String contactNoumber="";
                    String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    if(name!=null){
                        Log.e("Name :", name);
                        if(name.equalsIgnoreCase("011999999999999999")){
                            System.out.println("got it");
                        }
                    }
                    //to get the contact email
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    if(email!=null)
                        Log.e("Email", email);

                    String hasNoumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    if(Integer.parseInt(hasNoumber)>0){ 
                        Cursor pCur = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "
                                        +cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)),null, null);
                        int i = 0;
                        while (pCur.moveToNext()) {
                            //to get the contact number
                            contactNoumber = pCur.getString(pCur.getColumnIndex("DATA1"));
                            if(contactNoumber.equalsIgnoreCase("011999999999999999")){
                                System.out.println("got it");
                            }
                            contactNoumber = Constant.removeSpecialCharacters(contactNoumber);
                            Log.e("contactNoumber", contactNoumber);
                            // Getting Phone numbers
                            String numType = null;
                            if(pCur.getString(pCur.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){

                                switch(pCur.getInt(pCur.getColumnIndex("data2"))){
                                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
                                    numType = "HOME";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
                                    numType = "MOBILE";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
                                    numType = "WORK";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER :
                                    numType = "OTHER";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT :
                                    numType ="OTHER";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK :
                                    numType = "CALLBACK";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_CAR :
                                    numType ="CAR";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN :
                                    numType = "COMPANY MAIN";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME :
                                    numType = "FAX HOME";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK :
                                    numType = "FAX WORK";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN :
                                    numType = "MAIN";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN :
                                    numType = "ISDN";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_MMS :
                                    numType = "MMS";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX :
                                    numType = "OTHER FAX";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER :
                                    numType = "PAGER";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO :
                                    numType = "RADIO";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX :
                                    numType ="TELEX";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD :
                                    numType = "TTY TDD";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE :
                                    numType = "WORK MOBILE";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER :
                                    numType = "WORK PAGER";
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM :
                                    numType = "CUSTOM";
                                    break;
                                default:
                                    break;
                                }
                            }

                            numbers.add(i, contactNoumber+"("+numType+")");

                            i++;
                        }
                        String numInDB = null;
                        for (int j = 0; j < i; j++) {
                            if(j==0)
                                numInDB =numbers.get(j);
                            else
                                numInDB =numInDB + "," +numbers.get(j);
                        }
                        if(contactNoumber.length()>0){
                            if(name!=null){

                            }else{
                                name = contactNoumber;
                            }
                            callbackDB.InsertContacts(null, name+"="+numInDB, contactNoumber, email);
                        }
                    }
                } 
                cur1.close();
            }
            //CLOSE DB
            callbackDB.close();
        }
    }

解决方案

Below is the code which shows an easy way to read all phone numbers and names:

   Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
   while (phones.moveToNext())
   {
    String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  }
   phones.close();

Also For Sim contact only you can use below code:

 private void allSIMContact()
  {
    try
    {
        String m_simPhonename = null; 
        String m_simphoneNo = null;

        Uri simUri = Uri.parse("content://icc/adn"); 
        Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);

        Log.i("PhoneContact", "total: "+cursorSim.getCount());

        while (cursorSim.moveToNext()) 
        {      
            m_simPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
            m_simphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
            m_simphoneNo.replaceAll("\\D","");
            m_simphoneNo.replaceAll("&", "");
            m_simPhonename=m_simPhonename.replace("|","");

            Log.i("PhoneContact", "name: "+m_simPhonename+" phone: "+m_simphoneNo);
        }        
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

EDITED:

To get the details of the contacts like home,mobile,fax etc. you need to check for that individually as below:

        while (phone_crsr.moveToNext()) 
            { 
       int phone_type = phone_crsr.getInt(phone_crsr.getColumnIndex(Phone.TYPE));  
            switch (phone_type) 
            {    
            case Phone.TYPE_HOME: 
                 phone_home =phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 Toast.makeText(this, "home"+phone_home, Toast.LENGTH_LONG).show();
                 break;          
            case Phone.TYPE_MOBILE:    
                 phone_mob=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 Toast.makeText(this, "mob"+phone_mob, Toast.LENGTH_LONG).show();  
                 break;            
            case Phone.TYPE_WORK:                                
                 phone_work=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  Toast.makeText(this, "work"+phone_work, Toast.LENGTH_LONG).show();
                         break;           
             }
            }