1

I have created a class to find phone number in my contact.

It looks like this:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == RESULT_OK){
            if(requestCode == 1){
                Uri returnUri = data.getData();
                Cursor cursor = getContentResolver().query(returnUri, null,null,null, null);

                if(cursor.moveToNext()){
                    //get ID
                    int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                    String contactID = cursor.getString(columnIndex_ID);
                    //get name
                    String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    name.setText(contactName);

                    int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                    String hasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);

                    if(hasPhoneNumber.equals("1")){
                        Cursor cursorNum = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID,
                                null,
                                null
                        );
                        //get the first phone number
                        if(cursorNum.moveToNext()){
                            int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                            String stringNumber = cursorNum.getString(columnIndex_number);
                            phoneNumber.setText(stringNumber);
                        }
                    }else{
                        phoneReceiver.setText("No Phone Number");
                    }
                }else{
                    Toast.makeText(ActivityPager.this,"No Data!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

but, in my mind, i need to get the currently phone number in currently device that i use.

could i get it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sarudu Matra
  • 141
  • 1
  • 1
  • 6

1 Answers1

0

It's just a suggestion. If you want to get the contact name and details from the device means tou can do the following.

But you need to have stored the phone number on the SIM card, and there“s no other way to get your "own number".

  1. Read the phone number from SIM by using telephones manager.

    TelephonyManager tm = (TelephonyManager)this.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); String myPhoneNumber = tm.getLine1Number();

  2. You are reading all the phone numbers from your phone. So compare this and get other details.

Amsheer
  • 7,046
  • 8
  • 47
  • 81