Question

is there any way to get current username and mobile number in android ?

I want to get the user information of current phone number and username in android.

Any body have a code or idea,help me..Thank you..

Was it helpful?

Solution

Use this function to get the Owner information

 public String [] OwnerInfo() {

            final AccountManager manager = AccountManager.get(context);
            final Account[] accounts = manager.getAccountsByType("com.google");
            try{
            if (accounts[0].name != null) {


                //get the account_name
                ownerinfo[0] = accounts[0].name;

                ContentResolver cr = context.getContentResolver();
                Cursor emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.DATA + " = ?",
                        new String[] { ownerinfo[0] }, null);
                while (emailCur.moveToNext()) {
                    //get the id
                    ownerinfo[1] = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
                  //get the Email
                    ownerinfo[2] = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String newName = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (ownerinfo[3] == null || newName.length() > ownerinfo[3].length())
                         //get the Name
                         ownerinfo[3] = newName;

                    Log.v("Got contacts", "ID " + ownerinfo[1] + " Email : " + ownerinfo[2]
                            + " Name : " + ownerinfo[3]);
                }

                emailCur.close();
             // get the phone number
                final TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                ownerinfo[4] = tm.getLine1Number();

                if(ownerinfo[4]==null)
                {
                if (ownerinfo[1] != null) {

                    // get the phone number
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { ownerinfo[1] }, null);
                    while (pCur.moveToNext()) {
                        ownerinfo[4] = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.v("Got contacts", "phone" + ownerinfo[4]);
                    }
                    pCur.close();
                }
            }
            }
            }catch(ArrayIndexOutOfBoundsException e){
                Log.d("OwnerInfo","ArrayIndexOutOfBoundsException due to email account not available in your device");              
            }
            return ownerinfo;

        }

OTHER TIPS

You can use TelephonyManager for get mobile number.

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String number_User = tm.getLine1Number();

And also add permission in your manifest file:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>    

For that this will help you..

final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
  names[i] = accounts[i].name;
} 

You can use the TelephonyManager to do this:

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String number = tm.getLine1Number();

Also refer this answer.

The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be unavailable.

You need to add permission in your manifest file :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>    

For Owner name refer this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top