0

I'm using this function to get the Android phone number, without success, can some one give me a solution for this task?

My function:

public static String getPhoneDevice(Context mAppContext ){
    String strPhone = "";
    // metodo 1
    try{
        TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
        strPhone = tMgr.getLine1Number();
    }
    catch (Exception e)        {
    }

    // metodo 2
    if (strPhone.isEmpty()) {
        try{
            String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
            Object object = mAppContext.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;
                    strPhone = ((Cursor) (object)).getString(4);
                } while (true);
                ((Cursor) (object)).close();
            }
        }
        catch (Exception e)        {
        }
    }
    return  strPhone;
}

Manifest permission:

<!-- para acceso al numero telefono -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Glenn Pavel
  • 101
  • 1
  • 6
  • Possible duplicate of [getLine1Number() is not working](http://stackoverflow.com/questions/13251603/getline1number-is-not-working) – adelphus May 23 '16 at 21:05
  • thanks for your answer but i found that it works in some devices but fails in other.. – Glenn Pavel Oct 29 '16 at 01:38

0 Answers0