【问题标题】:is there any way to get current username and mobile number in android?有没有办法在android中获取当前的用户名和手机号码?
【发布时间】:2013-08-21 09:19:27
【问题描述】:

有没有办法在android中获取当前的用户名和手机号码?

我想在android中获取当前手机号码和用户名的用户信息。

任何机构有代码或想法,帮助我..谢谢..

【问题讨论】:

    标签: android telephonymanager


    【解决方案1】:

    使用该函数获取Owner信息

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

    【讨论】:

      【解决方案2】:

      您可以使用 TelephonyManager 获取手机号码。

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

      并且还在清单文件中添加权限:

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

      为此,这将对您有所帮助..

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

      【讨论】:

        【解决方案3】:

        您可以使用 TelephonyManager 执行此操作:

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

        另请参阅this answer

        documentation for getLine1Number() 表示如果号码“不可用”,此方法将返回 null,但没有说明号码何时可能不可用。

        您需要在清单文件中添加权限:

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

        所有者名称请参考this

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多