1

I'm very new to Xamarin and android development, I have developed simple widget which used in my company but I need to limit the app:

If there is email account on android device which have "@xyz.com" extension the app will work.

If there is no email with "@xyz.com" extension the app will display error message.

My question is how its possible to search all accounts in android device and detect the "@xyz.com" extension. If someone have example or piece of code it will be very helpful.

The closest example that I've found is:

http://androidexample.com/Get_Registered_Email_Accounts_-_Android_Example/index.php?view=article_discription&aid=110

But I didn't managed to convert it to Xamarin.

Thanks a lot,

David

Meleh
  • 23
  • 2
  • 1
    What is the error? Why didn't you manage to covert it to C#? What did you try yourself? Please read [this guide](http://stackoverflow.com/help/how-to-ask) on how a question should be formulated for your increasing your chances of getting an answer you can use. – Demitrian Jan 11 '17 at 22:15

1 Answers1

1

It's not quite difficult to convert java to c#, most APIs are encapsulate in Xamarin with the same name.

There are several methods to solve your problem. For example you can use AccountManager. To use AccountManager, you need open your AndroidManifest.xml under properties of your project and add this permission:

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

Then for example code like this:

var emialPattern = Patterns.EmailAddress;
var accounts = AccountManager.Get(this).GetAccounts();
foreach (var account in accounts)
{
    if (emialPattern.Matcher(account.Name).Matches())
    {
        string email = account.Name;
        string[] emailarray = email.Split('@');
        if (emailarray[1] == "xyz.com")
        {
            //TODO:
        }
        else
        {
            //TODO:
        }
    }
}

I suggest to read the answer of this case: How to get the Android device's primary e-mail address. Next time if you meet some problem while trying to convert Java code to c#, please show us what you've tried and post your code.

Community
  • 1
  • 1
Grace Feng
  • 16,564
  • 2
  • 22
  • 45