3

I am working in app. I need to get phone number programmatically. I follow this link

but i get phone number in one device, many times i am not getting .Have any perfect solution .

Community
  • 1
  • 1
Suman
  • 1,307
  • 15
  • 32
  • My link is correct solution? @ZahidulIslam – Suman Mar 31 '16 at 06:08
  • 2
    It's a solution but it's not perfect. It's not possible to get mobile number from android phone . Most of the phone don't allow you to get the mobile number . – Zahidul Islam Mar 31 '16 at 06:10
  • The best answer will be this http://stackoverflow.com/questions/2480288/programmatically-obtain-the-phone-number-of-the-android-phone/25131061#25131061 Ask it from the user itself.There are no other perfect solution. – Sunil Sunny Mar 31 '16 at 06:20
  • Do you want say take phone number from user manually? @sunilsunny – Suman Mar 31 '16 at 06:22
  • @Suman yes follow the link I shared .It's explaining it properly .Create an input field to enter the phone number on start of your app.Save this in sharedpreferance. – Sunil Sunny Mar 31 '16 at 06:26

1 Answers1

0

you can try this but not sure for all the device. It works only on some devices.

public static String getPhoneNumber(Context context) {
    String s1="";
    String main_data[] = {"data1", "is_primary", "data3", "data2", "data1",
            "is_primary", "photo_uri", "mimetype"};
    Object object = context.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");

    if (object != null) {
        do {
            if (!((Cursor) (object)).moveToNext())
                break;
            // This is the phoneNumber
            s1 =((Cursor) (object)).getString(4);
        } while (true);
        ((Cursor) (object)).close();
    }
    return  s1;
}
Aalishan Ansari
  • 641
  • 6
  • 8