Search Wikipedia

Search results

Jun 25, 2015

How to Send Email

We  do not need to develop our email client from scratch because they are already available like Gmail and K9mail. But we will need to send email from our android application, where we will have to write an Activity that need to launch an email client and sends an email using our android device. For this purpose, our Acivity will send an ACTION_SEND along with appropriate data load, to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send our email data.


STEP 1: INTENT OBJECT- Action to send Email


We will use ACTION_SEND action to launch an email client intalled on our Android device.
Following is simple syntax to create an intent with ACTION_SEND action.
Intent emailIntent = new Intent(Intent.ACTION_SEND);

STEP 2: INTENT OBJECT- Data/Type to send Email


To send an email we need to specify mailto: as URI using setData() method and datatype will be tottext/plain using setType() method as follows:
emailItent.setData(Uri.parse("mailto:"));
emailIntent.settype.settype("text/plain");

STEP 3: INTENT OBJECT- Extra to send Email


Android has built-in support to add TO,SUBJECT,CC,TEXT etc. fields which can be attached to intent before sending the intent to target email client.
Here is an example showing how to assign extra data to your intent:
emailIntent. putExtra(Intent.EXTRA_EMAIL,new String[]{"recipent@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"subject of email");
emailIntent.putExtra(Intent.EXTRA_TEXT,"body of email");

Jun 24, 2015

Create and Send Notifications

There is  a simple method  to create notification. Follow the following steps to create a notification.

STEP 1 - CREATE NOTIFICATION BUILDER

As a first step is to create a notification builder using NotificationCompat.Builder.build(). We will use Notification to set various Notification properties like its small and large icons,title,priority etc.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)

STEP 2 - SETTING NOTIFICATION PROPERTIES

Once we have Builder object, we can set its Notification properties using Builder object as per our requirement. But this is mandatory to set atleast following:
  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuiler.setContentTitle("Notification Alert, Click Me !");
mBuilder.setContentText("Hi, This is Android Notification Detail");

STEP 3 - ATTACH ACTIONS

This is an optional part and required if we want to attach an action with the application. An action allows users to go directly from the notification to an Activity in our application, where user can look at one or more events or do further work.

The action is defined by a PendingIntent containing an Intent that starts an Activity in our application. To asscociate the PendingIntent with a gesture, call the appropriate method of NotificationBuilder.Builder. For example, if we want to start Activity when user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().

A PendingIntent object helps us to perform an action on our application's behalf, often at a later time, without caring of whether or  not our application is running.

We can help of stack builder object which will contain an artificial back stack for the started Activity. This ensure the navigating backward from the Activity leads out of our application to the Home screen.

Intent resultIntent = new Intent(this,ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
//Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mbuilder.setCotentIntent(resultPendingIntent);

STEP 4 - ISSUE THE NOTIFICATION

Finally, we can pass the Notification object to the system by calling NotificationManager.notifiy() to send our notification. Make sure we call NotificationCompat.Builder.build() method on builder object before notifying it. This method combines all of the options that have been set and return a new Notification object.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//notificatioID allows us to update the notification later on 
mNotificationManager.notify(notificationID, mBuilder.build());

}