0% found this document useful (0 votes)
10 views2 pages

Exp 11

The document outlines an Android application that generates a notification alert when a message is received. It includes Java code for the MainActivity class, which sets up a button to trigger the notification with user input from an EditText field. Additionally, it provides the XML layout for the user interface, featuring a TextView, EditText, and Button.

Uploaded by

amishav2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Exp 11

The document outlines an Android application that generates a notification alert when a message is received. It includes Java code for the MainActivity class, which sets up a button to trigger the notification with user input from an EditText field. Additionally, it provides the XML layout for the user interface, featuring a TextView, EditText, and Button.

Uploaded by

amishav2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment-11

To Implement an Android Application that creates an alert upon receiving a message.

Program:
package com.example.mcexp10;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import
android.app.Notification;
import
android.app.NotificationMan
ager; import
android.app.PendingIntent;
import
android.content.Intent;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;

public class MainActivity extends AppCompatActivity


{
Button notify;
EditText
e;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notify= (Button)
findViewById(R.id.button); e=
(EditText) findViewById(R.id.editText);

notify.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
PendingIntent pending =
PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Notification noti = new
Notification.Builder(MainActivity.this).setContentTitle("New
Message").setContentText(e.getText().toString()).setSmallIcon(R.
mipmap.ic_l auncher).setContentIntent(pending).build();
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(0, noti);
}
});

}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/r
es/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp"/>

</LinearLayout>

You might also like