Aim:

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

Procedure:

Creating a New project:

  • Open Android Studio and then click on File -> New -> New project.

new project

  • Then type the Application name as “ex.no.10″ and click Next. 

application-name-10

  • Then select the Minimum SDK as shown below and click Next.

minimum sdk

  • Then select the Empty Activity and click Next. 

empty activity

  • Finally click Finish.

finish

  • It will take some time to build and load the project.
  • After completion it will look as given below.

new

Creating Second Activity for the Android Application:

  • Click on File -> New -> Activity -> Empty Activity.

New activity

  • Type the Activity Name as SecondActivity and click Finish button.

SecondActivity

  • Thus Second Activity For the application is created.

Designing layout for the Android Application:

  • Click on app -> res -> layout -> activity_main.xml.

activity_main

  • Now click on Text as shown below.

text

  • Then delete the code which is there and type the code as given below.

Code for Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/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>
  • Now click on Design and your application will look as given below.

design-10

  • So now the designing part is completed.

Java Coding for the Android Application:

  • Click on app -> java -> com.example.exno10 -> MainActivity.

MainActivity

  • Then delete the code which is there and type the code as given below.

Code for MainActivity.java:

package com.example.exno10; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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_launcher).setContentIntent(pending).build(); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); noti.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(0, noti); } }); }
}
  • So now the Coding part is also completed.
  • Now run the application to see the output.

Output:

android application  Screenshot_2016-03-16-00-41-32

Screenshot_2016-03-16-00-41-44  Screenshot_2016-03-16-00-41-53

Result:

              Thus Android Application that creates an alert upon receiving a message is developed and executed successfully.

Similar Posts