You are on page 1of 11

Ex.

No : 10
CREATES AN ALERT UPON RECEIVING A MESSAGE
Date :
AIM :
To implement an application that creates an alert upon receiving a message
Procedure and Program:
(Note: Internet Connection is Mandatory to run this application)
Step 1 : Open Eclipse from Android ADT Bundle
Step 2 : Go to File Menu Choose New Android Application Project

Step 3 : In the displayed window type Application name as Ex.No 10 , and click Next 4 times.
Step 4 : In the Last window type Activity Name and Layout Name, and press Finish

Step 5 : For layout design, Choose res/layout/activity_main.xml file


Step 6 : We can design the layout in two way, first way using graphical layout using drag and
drop option, second way is using XML Code.
Step 7 : a. Using Graphical layout drag and drop the following Components
activity_main.xml

OR
Step 7 : b. Using XML code Choose activity_main.xml,

type the below Code


activity_main.xml (File#1)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/btnGenerateNotifications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="186dp"
android:text="Click me to Generate a Notification" />

</RelativeLayout>

sub_main.xml (File#2)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SubActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Android 6.0 or Marshmallow is high in the news these
days. These headlines about Marshmallow are making people think whether their
current Android device will be upgraded to the latest version or they will be
left behind. A basic measure would be that most of the devices which came
with Android 5.0 (Lollipop) out of the box will be updated to the latest
Android 6.0. This post tries to list up the devices for which Android 6.0
update is imminent."
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Step 8 : For Java Code (Action), Choose src/com.example.ex.no 10/MainActivity.java


type the below Code
MainActitivity.java (File #1)
package com.example.ex.no10;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.btnGenerateNotifications);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,
SubActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent, 0);

NotificationCompat.Builder builder = new


NotificationCompat.Builder(MainActivity.this);
/* builder.setContentTitle("Normal Regular
Notification");

builder.setContentText("ContentText,ContentText,ContentText,ContentText
,ContentText,ContentText,ContentText");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("Message from
Developer.Android....");
builder.setAutoCancel(true);
builder.setContentIntent(pendIntent);*/

Notification notif = builder.build();


NotificationManager nm =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(234, notif);
Notification notiMail = new Notification.Builder(
MainActivity.this)
.setTicker("Message from
Developer.Android....")

.setContentTitle("Marshmallow@developer.android.in")
.setContentText("Android 6.0")
.setSmallIcon(R.drawable.ic_launcher)

.setContentIntent(pendIntent).setAutoCancel(true)
.build();

NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, notiMail);

}
});

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

SubActitivity.java (File #2)

package com.example.ex.no10;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class SubActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.sub, menu);
return true;
}

Step 9: Save All and Run it as Android Application (Ctrl + F11) .


OUTPUT:

RESULT:
Thus the above android application has been run successfully.

You might also like