zaterdag 14 december 2013

Get Registered Email Accounts

In this example getting registered email Accounts in android phone.
Getting primary(google) mail account and all registered acconts. Showing accounts on screen.



NOTE :

This android example will work in real device.


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

Java File: RegisteredEmailAccounts.java

Call getAccounts() method from AccountManager class to get all registered email accounts on android phone.

import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
 
public class RegisteredEmailAccounts extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
        setContentView(R.layout.registered_email_account);
        final TextView accountsData = (TextView) findViewById(R.id.accounts);
         
        String possibleEmail="";
         
           try{
                   possibleEmail += "************* Get Registered Gmail Account *************nn";
                   Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
                    
                   for (Account account : accounts) {
                      
                     possibleEmail += " --> "+account.name+" : "+account.type+" , n";
                     possibleEmail += " nn";
                      
                   }
              }
              catch(Exception e)
              {
                   Log.i("Exception", "Exception:"+e) ;
              }
               
               
              try{
                   possibleEmail += "**************** Get All Registered Accounts *****************nn";
                   
                   Account[] accounts = AccountManager.get(this).getAccounts();
                   for (Account account : accounts) {
                      
                      possibleEmail += " --> "+account.name+" : "+account.type+" , n";
                      possibleEmail += " n";
                      
                   }
              }
              catch(Exception e)
              {
                   Log.i("Exception", "Exception:"+e) ;
              }
            
           // Show on screen   
           accountsData.setText(possibleEmail);
            
           Log.i("Exception", "mails:"+possibleEmail) ;
    }
 
     
}

0 reacties:

Een reactie plaatsen