Search code examples
androidtelephonymanager

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


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..


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;
    
            }