Download Code

In this tutorial you will learn how to find attach email’s with your android device.Before we get started i’ll give some information about classes which we will use in that project.

App Email Finder

Pattern

Pattern is used for regular expression’s which means to match with regular expression’s which stored on your android device.Different fields of Pattern’s are.

  • DOMAIN_NAME
  • EMAIL_ADDRESS
  • IP_ADDRESS
  • PHONE
  • WEB_URL
  • TOP_LEVEL_DOMAIN

Public Methods

  • concateGroups(Matcher matcher)
  • digitsAndPlusOnly(Matcher matcher)

for more detail.

If you are not familiar with list you can go through this tutorial.

Account

Account gives value type which represent an account in the AccountManager class Further Detail.

Account Manager

Account Manager class is used to provide access to the user’s online accounts.This class is also used to handle every authentication of owner of that account.

So let’s start go ahead and create android application project.

email

First you will design your xml layout with two buttons and one image view for email app.Your xml and Graphical layout will look like this.

XML

email xml

Graphical

graphical email

Now done with xml and graphical layout.Next go to your MainActivity.java class create variable’s for button’s.Then apply click event on both button’s.After that MainActivity.java will look like this.

package androidcodec.emailfinder;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

	// email list button and exit button variables
	Button emaillist,exit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//getting reference id of emaillist button
		emaillist = (Button) findViewById(R.id.email);
		//click event on Load Emails button
		emaillist.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				
			}
		});
		//getting reference of exit button
		exit = (Button) findViewById(R.id.exit);
		//click event on exit button
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub		
			}
		});			
	}	
}

Next write one line to exit from your application within click event of exit button like this.

package androidcodec.emailfinder;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

	// email list button and exit button variables
	Button emaillist,exit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//getting reference id of emaillist button
		emaillist = (Button) findViewById(R.id.email);
		//click event on Load Emails button
		emaillist.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		//getting reference of exit button
		exit = (Button) findViewById(R.id.exit);
		//click event on exit button
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.exit(0); // exit from application
			}
		});
				
	}	
}

Now you are done with exit button.Next you will deal with Load Emails button.First create new class and name it emailList or whatever you want.If you don’t know how to create new class just go through this tutorial.When your display in front of you.It will look like this.

package androidcodec.emailfinder;
public class emailList{
}

Now don’t forget to declare that activity in Android Manifest.Next extends emailList class with ListActivity class.After that your emailList class will look like this.

package androidcodec.emailfinder;
public class emailList{
        @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        }
        @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	
	}
}

Now you have created email display list activity.Now put code in MainActivity Load Emails button to start this activity(emailList) to display emails.You will start activity using intent then your code will look like that.
Complete MainActivity.java

package androidcodec.emailfinder;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

	// email list button and exit button variables
	Button emaillist,exit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//getting reference id of emaillist button
		emaillist = (Button) findViewById(R.id.email);
		//click event on Load Emails button
		emaillist.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// starting a new activity emailList.class to display email list
				startActivity(new Intent(MainActivity.this,emailList.class));
			}
		});
		//getting reference of exit button
		exit = (Button) findViewById(R.id.exit);
		//click event on exit button
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.exit(0); // exit from application
			}
		});			
	}	
}

Next in emailList class declare List of string like this.

// declaring list of string type which will be an array list of string type
List classes = new ArrayList();

Now next you will use setlistAdapter method to display list like.

// setting an array to arrayadapter and set it to list adapter works just like setContentView()
setListAdapter(new ArrayAdapter(emailList.this,android.R.layout.simple_list_item_1,classes));

Next create pattern variable and also point to the Field EMAIL_ADDRESS like that.

// created a pattern variable to identify email address
		Pattern emailPattern = Patterns.EMAIL_ADDRESS; // added in API level 8+

Then create Accounts array to store to list of accounts and match pattern variable with account to match with regular expression’s of email’s means rules.Then at last get possible email address and pass it to list which you called classes.Final code of emailList will be.
Email List class

package androidcodec.emailfinder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class emailList extends ListActivity {
	// declaring list of string type which will be an array list of string type
	List classes = new ArrayList();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// setting an array to arrayadapter and set it to list adapter works just like setContentView()
		setListAdapter(new ArrayAdapter(emailList.this,android.R.layout.simple_list_item_1,classes));
		// created a pattern variable to identify email address
		Pattern emailPattern = Patterns.EMAIL_ADDRESS; // added in API level 8+
		// account value array
		Account[] accounts = AccountManager.get(getBaseContext()).getAccounts(); // getting list of accounts in device
		// associated loop of account
		for (Account account : accounts) {
			// condition to match pattern for email address
		    if (emailPattern.matcher(account.name).matches()) {
		    	//getting possible email pattern and store in variable
		        String possibleEmail = account.name;
		        //providing access to all online accounts in device
		        AccountManager manager = AccountManager.get(this);
		        // displaying all email's in toast which founded
		        Toast.makeText(getApplicationContext(), possibleEmail , Toast.LENGTH_SHORT).show();
		        // adding every email in list
		        classes.add(possibleEmail);
  
		    }
		}
	} // end of oncreate
	
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	
	}
	

}

Android Manifest.xml
manifest

Now run your android application project.Final Output will looks like this.
out
After clicking Load Emails button it will display list of accounts.
final email list
I hope you found this tutorial helpfull.Don’t forget to leave your feedback.
Thanks.