在Xamarin中获取用户名

eqqqjvef  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(251)

Does anyone know, how to get the username in Android using C# Xamarin?
I mean, when you in Android open:
Settings > System > About phone > [Name at the top] ?
Or:
Settings > System > About phone > Multiple users > You (the name) ?
I'm only interested in the user (display) name and not the e-mail nor the device name (which is not the same as the username).
I've tried with await Contacts.GetAllAsync() using the required permissions, but the result was always empty.
Then I've tried with Environment.UserName , but this returned "somebody"...
I did also found this post , but my code not find either the User class or the CrossCurrentActivity class that is mentioned in the code in the post...
Also the post describes installing a CurrentActivity plugin, but the page doesn't exist anymore...
I'm using Xamarin Forms with Shell (latest version) and Visual Studio 2019 Enterprise.
My Android version is 9.0.
Thank you.

UPDATE:

See this picture from the settings of my Android emulated device for explanation of, what I mean:

tzdcorbm

tzdcorbm1#

根据您的需要,Xamarin.Essentials提供了一种特殊的方法来获取您的设备信息
1.下载Xamarin。Nuget中的必备软件。
1.使用var deviceName = DeviceInfo.Name;来取得。
有关详细信息,请参阅https://learn.microsoft.com/en-us/xamarin/essentials/device-information?tabs=ios

pkwftd7m

pkwftd7m2#

public class UserInformation : IUserInformation
    {
        /*  Caution: This method returns personal and sensitive user data.If your app accesses, collects, uses, or shares personal and sensitive data, 
         *  you must clearly disclose that fact to users. For apps published on Google Play, policies protecting user data require that you do the following:
         *  Disclose to the user how your app accesses, collects, uses, or shares personal and sensitive data. Learn more about acceptable disclosure and consent.
         * Provide a privacy policy that describes your use of this data on- and off-device.
         * To learn more, visit the Google Play Policy regarding user data. */

        public Task<string> GetUserName()
        {
            FormsAppCompatActivity activity = CrossCurrentActivity.Current.Activity as FormsAppCompatActivity;

            AccountManager manager = AccountManager.Get(activity);
            Account[] accounts = manager.GetAccounts();     // .GetAccountsByType("com.google");   "Type = "com.osp.app.signin" ?
            List<String> emails = new List<string>();

            foreach (Account account in accounts)
            {
                //var name = account.Name;
                emails.Add(account.Name);
            }

            // Should Loop until find one with "name"
            if (emails.Any() && emails[0] != null)
            {
                String email = emails[0];
                String[] parts = email.Split('@');

                if (parts.Length > 1)
                {
                   return Task.FromResult(parts[0]);
                }
            }

            return Task.FromResult("");
        }
    }

相关问题