Fetch Local phonebook contacts From SIM card only android

后端 未结 2 1000
故里飘歌
故里飘歌 2021-01-07 09:08

I want to know if its possible to fetch contacts which exist in SIM card or phonebook only. Right now I am using the following code to fetch contacts and it fetches all the

相关标签:
2条回答
  • 2021-01-07 09:09

    For Sim contact only you can use below code

    private void allSIMContact()
        {
            try
            {
                String ClsSimPhonename = null; 
                String ClsSimphoneNo = 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()) 
                {      
                    ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
                    ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
                    ClsSimphoneNo.replaceAll("\\D","");
                    ClsSimphoneNo.replaceAll("&", "");
                    ClsSimPhonename=ClsSimPhonename.replace("|","");
    
                    Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo);
                }        
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
    0 讨论(0)
  • 2021-01-07 09:11
    Cursor cursor = getContacts();
        getBundleValues2();
    
        String displayName = "";
        while (cursor.moveToNext()) {
            //taking id name and phone number from contacts using content provider
            String displayid = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
    
            displayName = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    
            String displayphnno = "";
    
            ContentResolver cr = getContentResolver();
    
            if (Integer.parseInt(cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER))) > 0) {
    
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { displayid }, null);
                while (pCur.moveToNext()) {
                    displayphnno = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                            + "\n";
    
                    ContactProviderViewGroup v6;
                    //using view group appending it to an activity
                    if (colorFlag) {
                        v6 = new ContactProviderViewGroup(this, displayName,
                                displayphnno, passedid, colorFlag);
                        colorFlag = false;
                    } else {
                        v6 = new ContactProviderViewGroup(this, displayName,
                                displayphnno, passedid, colorFlag);
                        colorFlag=true;
                    }
                    LL1.addView(v6);
                }
                pCur.close();
            }
    
        }
    
    }
    
    private void getBundleValues2() {
        Intent i = getIntent();
        Bundle myBundle = i.getExtras();
    
        if (myBundle != null) {
            passedid = myBundle.getInt("gid");
        }
    }
    
    private Cursor getContacts() {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
    
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER };
    
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
                + ("1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
    
        return managedQuery(uri, projection, selection, selectionArgs,
                sortOrder);
    }
    
    0 讨论(0)
提交回复
热议问题