Implementation of an Android Application That Creates Alert on Receiving a Message: Full Code in Android

Implementation of an Android Application That Creates Alert on Receiving a Message: Full Code in Android
Implementation of an Android Application That Creates Alert on Receiving a Message: Full Code in Android

Android AlertDialog:
  1. It is used to display the dialog message with the OK and Cancel button.
  2. It is used to ask the user about his choice to continue or discontinue.
  3. It contains three areas: Title, content area, and the action buttons.
Methods used are:
  1. public AlertDialog.BuildersetTitle(CharSequence): It is used to set the title of AlertDialog.
  2. public AlertDialog.BuildersetMessage(CharSequence): It is used to set the message for AlertDialog.
  3. public AlertDialog.BuildersetIcon(int): it is used to set the icon over AlertDialog.
Code:

 MainActivity.java:
   
package com.example.znotifier;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button btton;
EditTexted;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btton= (Button) findViewById(R.id.notify);
ed= (EditText) findViewById(R.id.message);

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


zact.java :

packagecom.example.znotifier;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class zact extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zact);
    }
}

Activity_main.XML:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#509FDF"
android:foregroundTint="#4496D8"
tools:context=".MainActivity">

<EditText
android:id="@+id/message"
android:layout_width="270dp"
android:layout_height="54dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Message"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063" />

<Button
android:id="@+id/notify"
android:layout_width="132dp"
android:layout_height="58dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="#77A8D3"
android:text="Notify"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.283" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity_zact.XML:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".zact">

</androidx.constraintlayout.widget.ConstraintLayout>




Post a Comment

0 Comments