Article From:https://www.cnblogs.com/819158327fan/p/9305309.html

Recently, the company made a loan APP, asked the background to secretly access users’ contacts to do wind control, so. (everyone ignores it well)

  1. Access to mobile phone contacts is very simple, that is, query Android database, using ContentProvider for cross process communication. I’m going directly to the code
    • public List<ContactsResultBean.ContactsBean> getContactInfo() throws JSONException {
              list = new ArrayList<>();
              contactData = new JSONObject();
              //The Uri of the contact, that is, content://com.android.contacts/contacts
              Uri uri = ContactsContract.Contacts.CONTENT_URI;
              //Specifying data for _id and display_name two columns, display_name is the name.
              String[] projection = new String[]{
                      ContactsContract.Contacts._ID,
                      ContactsContract.Contacts.DISPLAY_NAME
              };
              //According to Uri query the corresponding ContentProvider, cursor is the data set to get.
              Cursor cursor = context.getContentResolver().
                      query(uri, projection, null, null, null);
      
              int i = 0;
              if (cursor != null && cursor.moveToFirst()) {
                  do {
                      Long id = cursor.getLong(0);
                      //Get a name
                      String name = cursor.getString(1);
                      //Specify the column data to get the NUMBER
                      String[] phoneProjection = new String[]{
                              ContactsContract.CommonDataKinds.Phone.NUMBER
                      };
      
                      //Get the person's phone number based on the ID of the contact
                      Cursor phonesCusor = context.getContentResolver().query(
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                              phoneProjection,
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
                              null,
                              null);
      
                      //Because each contact may have multiple phone numbers, it needs to be traversed.
                      if (phonesCusor != null && phonesCusor.moveToFirst()) {
                          do {
                              String num = phonesCusor.getString(0);
      
                              contactsBean = new ContactsResultBean.ContactsBean();
                              contactsBean.setName(name);
                              contactsBean.setPhone(num);
                              contactsBean.setNote("");
      
                              list.add(contactsBean);
                              jsonObject = new JSONObject();
                              contactData.put("contact" + i, jsonObject);
      
                          } while (phonesCusor.moveToNext());
                      }
                      i++;
                  } while (cursor.moveToNext());
              }
      
              // Access to the contact --1 of the SIM card
              try {
                  getSimContact("content://icc/adn", list);
      
                  getSimContact("content://icc/adn/subId/#", list);
      
                  getSimContact("content://icc/sdn", list);
      
                  getSimContact("content://icc/sdn/subId/#", list);
      
                  getSimContact("content://icc/fdn", list);
      
                  getSimContact("content://icc/fdn/subId/#", list);
      
              } catch (Exception e) {
                  Log.d("It comes out: ", e.getLocalizedMessage());
              }
              return list;
          }

       

  2、The connection on the query Sim is also very simple, it is also a scan of the value stored in the Android database, but also through the ContentProvider, but because some mobile phones may be double card and double, there are three kinds of mobile, telecommunication and Unicom, so there may be a problem.That’s right.

    Here I know that the Android framework itself provides such a provider (IccProvider. java), so let’s follow Google’s instructions. The most important way to use ContentProvider is the roadTo be accurate, let’s take a look at the routing provided to me in IccProvider.

    • First look at the routing provided to us by IccProvider.

 

    The same is also returned to our cursor, which we value from the cursor.

    The following is the code for the value of the cursor

    

    private void getSimContact(String adn, List<ContactsResultBean.ContactsBean> list) {
        // Reading the SIM card phone number, there are three possibilities: content://icc/adn || content://icc/sdn || content://icc/fdn
        // A specific view of class IccProvider.java
        Cursor cursor = null;
        try {
            Uri uri = Uri.parse(adn);
            LogUtils.e(uri.toString());
            cursor = context.getContentResolver().query(uri, null,
                    null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    // Get the name of the contact
                    int nameIndex = cursor.getColumnIndex("name");
                    // Get the phone number
                    int numberIndex = cursor.getColumnIndex("number");
                    String number = cursor.getString(numberIndex);// Cell-phone number
                    LogUtils.d("Cell phone number: "+ number);
                    if (isUserNumber(number)) {// Is it a mobile phone number
                        ContactsResultBean.ContactsBean simCardTemp = new ContactsResultBean.ContactsBean();
                        simCardTemp.setPhone(formatMobileNumber(number));
                        simCardTemp.setName(cursor.getString(nameIndex));
                        if (!list.contains(simCardTemp)) {
                            list.add(simCardTemp);
                        }
                    }
                }
                cursor.close();
            }
        } catch (Exception e) {
            LogUtils.e(e);
            if (cursor != null) {
                cursor.close();
            }
        }
    }

When tested, you can go to the contacts on the SIM card.

One Reply to “Android– access to phone contacts and Sim card contacts”

  1. I am 54 years old so I am not tech savvy. I have only 1 casino site ( house of fun ). I am being swamped by other sites that kick in whenever I go to my AOL mail and sometimes my Gmail. I am so, so your of this intrusion. It sucks up my data. I’m so frustrated. I have a ANS UL40 Assurance wireless phone . Please don’t give me alot of technical mumbo jumbo. Straight easy word’s.. laugh All you want but when you get older you’ll be just as lost at that current technology. Get these damn game sites off my phone !!!!!!;

Leave a Reply

Your email address will not be published. Required fields are marked *