-2

I am using this method to get the device number.

TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimNumber = telemamanger.getLine1Number();

but this method only works in android 7.0 and not in 6.0 and 8.0 please suggest me how i will get the mobile number in Android version 6.0 and 8.0

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Arjun
  • 25
  • 1
  • https://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone – AskNilesh Sep 14 '18 at 04:51
  • 1
    Possible duplicate of [Programmatically obtain the phone number of the Android phone](https://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone) – Vikasdeep Singh Sep 14 '18 at 04:52

1 Answers1

0
 private void showContacts() {
    // Check the SDK version and whether the permission is already granted or not.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
        //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
    } else {
        String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
        Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
                main_data, "mimetype=?",
                new String[]{"vnd.android.cursor.item/phone_v2"},
                "is_primary DESC");
        String s1="";
        if (object != null) {
            do {
                if (!((Cursor) (object)).moveToNext())
                    break;
                // This is the phone Number
                s1 = ((Cursor) (object)).getString(4);
                Log.i("TestMOB",s1);
            } while (true);
            ((Cursor) (object)).close();
        }

    }
}

Hope this will hlp u:)

Archu Mohan
  • 199
  • 1
  • 5
  • 14