You are on page 1of 130

CS6611 MOBILE APPLICATION DEVELOPMENT LABORATORY LTPC

0032
OBJECTIVES:

The student should be made to:


 Know the components and structure of mobile application development frameworks for
Android and windows OS based mobiles.
 Understand how to work with various mobile application development frameworks.
 Learn the basic and important design concepts and issues of development of mobile
applications.
 Understand the capabilities and limitations of mobile devices.

LIST OF EXPERIMENTS:

1. Develop an application that uses GUI components, Font and Colours


2. Develop an application that uses Layout Managers and event listeners.
3. Develop a native calculator application.
4. Write an application that draws basic graphical primitives on the screen.
5. Develop an application that makes use of database.
6. Develop an application that makes use of RSS Feed.
7. Implement an application that implements Multi threading
8. Develop a native application that uses GPS location information.
9. Implement an application that writes data to the SD card.
10. Implement an application that creates an alert upon receiving a message.
11. Write a mobile application that creates alarm clock

TOTAL: 45 PERIODS
OUTCOMES:
At the end of the course, the student should be able to:
 Design and Implement various mobile applications using emulators.
 Deploy applications to hand-held devices

LIST OF EQUIPMENT FOR A BATCH OF 30 STUDENTS:


Standalone desktops with Windows or Android or iOS or Equivalent Mobile Application
Development Tools with appropriate emulators and debuggers - 30 Nos.
Ex.No: 1 - Android Application that uses GUI components, Font and Colors
Aim:

To develop a Simple Android Application that uses GUI components, Font and Colors.

Procedure:

Creating a New project:

 Open Android Stdio and then click on File -> New -> New project.

 Then type the Application name as “ex.no.1″ and click Next.


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

 Then select the Empty Activity and click Next.


 Finally click Finish.

 It will take some time to build and load the project.


 After completion it will look as given below.
Designing layout for the Android Application:

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

 Now click on Text as shown below.


 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/textView" android:layout_width="match_parent"


android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Java Coding for the Android Application:

 Click on app -> java -> com.example.exno1 -> MainActivity.


 Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
package com.example.exno1;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity


{
int ch=1;
float font=30;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t= (TextView) findViewById(R.id.textView);
Button b1= (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setTextSize(font);
font = font + 5;
if (font == 50)
font = 30;
}
});
Button b2= (Button) findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (ch) {
case 1:
t.setTextColor(Color.RED);
break;
case 2:
t.setTextColor(Color.GREEN);
break;
case 3:
t.setTextColor(Color.BLUE);
break;
case 4:
t.setTextColor(Color.CYAN);
break;
case 5:
t.setTextColor(Color.YELLOW);
break;
case 6:
t.setTextColor(Color.MAGENTA);
break;
}
ch++;
if (ch == 7)
ch = 1;
}
});
}
}
 So now the Coding part is also completed.
 Now run the application to see the output.

Output:
Result:

Thus a Simple Android Application that uses GUI components, Font and Colors is
developed and executed successfully.
Ex.No: 2 Develop an application that uses Layout Managers and event listeners.

Aim:

To develop a Simple Android Application that uses Layout Managers and Event Listeners.

Procedure:

Creating a New project:

 Open Android Stdio and then click on File -> New -> New project.

 Then type the Application name as “ex.no.2″ and click Next.


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

 Then select the Empty Activity and click Next.


 Finally click Finish.

 It will take some time to build and load the project.


 After completion it will look as given below.
Creating Second Activity for the Android Application:

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

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


 Thus Second Activity For the application is created.

Designing layout for the Android Application:

Designing Layout for Main Activity:

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

 Now click on Text as shown below.


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

<?xml version="1.0" encoding="utf-8"?>

<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" tools:context=".MainActivity">

<LinearLayout android:layout_width="match_parent" android:layout_height="100dp">

<TextView android:id="@+id/textView" android:layout_width="match_parent"


android:layout_height="wrap_content" android:layout_margin="30dp" android:text="Details
Form" android:textSize="25sp" android:gravity="center"/>

</LinearLayout>

<GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent"


android:layout_height="match_parent" android:layout_marginTop="100dp"
android:layout_marginBottom="200dp" android:columnCount="2" android:rowCount="3">

<TextView android:id="@+id/textView1" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="0" android:layout_column="0" android:text="Name"
android:textSize="20sp" android:gravity="center"/>
<EditText android:id="@+id/editText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="0" android:layout_column="1" android:ems="10"/>

<TextView android:id="@+id/textView2" android:layout_width="wrap_content"


android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_row="1" android:layout_column="0"
android:text="Reg.No" android:textSize="20sp" android:gravity="center"/>

<EditText android:id="@+id/editText2" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="1" android:layout_column="1" android:inputType="number"
android:ems="10"/>

<TextView android:id="@+id/textView3" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="2" android:layout_column="0" android:text="Dept"
android:textSize="20sp" android:gravity="center"/>

<Spinner android:id="@+id/spinner" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_row="2"

android:layout_column="1" android:spinnerMode="dropdown"/>

</GridLayout>

<Button android:id="@+id/button" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:layout_centerInParent="true" android:layout_marginBottom="150dp"
android:text="Submit"/>

</RelativeLayout>

Code for Activity_main.xml:

 Now click on Design and your activity will look as given below.
 So now the designing part of Main Activity is completed.

Designing Layout for Second Activity:

 Click on app -> res -> layout -> activity_second.xml.

 Now click on Text as shown below.


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

Code for Activity_second.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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"

tools:context="com.example.devang.exno2.SecondActivity"

android:orientation="vertical"

android:gravity="center">

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_margin="20dp"

android:text="New Text"

android:textSize="30sp"/>

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:text="New Text"

android:textSize="30sp"/>

<TextView

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="20dp"

android:text="New Text"

android:textSize="30sp"/>

</LinearLayout>
 Now click on Design and your activity will look as given below.

 So now the designing part of Second Activity is also completed.

Java Coding for the Android Application:

Java Coidng for Main Activity:

 Click on app -> java -> com.example.exno2 -> MainActivity.

 Then delete the code which is there and type the code as given below.
Code for MainActivity.java:

package com.example.exno2;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

//Defining the Views

EditText e1,e2;

Button bt;

Spinner s;

//Data for populating in Spinner

String [] dept_array={"CSE","ECE","IT","Mech","Civil"};
String name,reg,dept;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Referring the Views

e1= (EditText) findViewById(R.id.editText);

e2= (EditText) findViewById(R.id.editText2);

bt= (Button) findViewById(R.id.button);

s= (Spinner) findViewById(R.id.spinner);

//Creating Adapter for Spinner for adapting the data from array to Spinner

ArrayAdapter adapter= new


ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,dept_array);

s.setAdapter(adapter);

//Creating Listener for Button

bt.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//Getting the Values from Views(Edittext & Spinner)

name=e1.getText().toString();

reg=e2.getText().toString();

dept=s.getSelectedItem().toString();

//Intent For Navigating to Second Activity

Intent i = new Intent(MainActivity.this,SecondActivity.class);

//For Passing the Values to Second Activity

i.putExtra("name_key", name);

i.putExtra("reg_key",reg);

i.putExtra("dept_key", dept);

startActivity(i);

});

 So now the Coding part of Main Activity is completed.


Java Coding for Second Activity:

 Click on app -> java -> com.example.exno2 -> SecondActivity.

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

Code for SecondActivity.java:

package com.example.exno2;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

TextView t1,t2,t3;
String name,reg,dept;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

t1= (TextView) findViewById(R.id.textView1);

t2= (TextView) findViewById(R.id.textView2);

t3= (TextView) findViewById(R.id.textView3);

//Getting the Intent

Intent i = getIntent();

//Getting the Values from First Activity using the Intent received

name=i.getStringExtra("name_key");

reg=i.getStringExtra("reg_key");

dept=i.getStringExtra("dept_key");

//Setting the Values to Intent

t1.setText(name);

t2.setText(reg);
t3.setText(dept);

 So now the Coding part of Second Activity is also completed.


 Now run the application to see the output.

Output:
Result:

Thus a Simple Android Application that uses Layout Managers and Event Listeners is
developed and executed successfully.

Ex.No:3. Develop a native calculator application.

Aim:
To develop a Simple Android Application for Native Calculator.
Procedure:
Creating a New project:

 Open Android Stdio and then click on File -> New -> New project.

 Then type the Application name as “ex.no.3″ and click Next.

 Then select the Minimum SDK as shown below and click Next.
 Then select the Empty Activity and click Next.

 Finally click Finish.


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

Designing layout for the Android Application:

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


 Now click on Text as shown below.

 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>

<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>

<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>

<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>
</LinearLayout>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Answer is"
android:textSize="30sp"
android:gravity="center"/>

</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Java Coding for the Android Application:

 Click on app -> java -> com.example.exno3 -> MainActivity.


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

Code for MainActivity.java:

package com.example.devang.exno3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements


OnClickListener
{
//Defining the Views
EditText Num1;
EditText Num2;
Button Add;
Button Sub;
Button Mul;
Button Div;
TextView Result;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Referring the Views


Num1 = (EditText) findViewById(R.id.editText1);
Num2 = (EditText) findViewById(R.id.editText2);
Add = (Button) findViewById(R.id.Add);
Sub = (Button) findViewById(R.id.Sub);
Mul = (Button) findViewById(R.id.Mul);
Div = (Button) findViewById(R.id.Div);
Result = (TextView) findViewById(R.id.textView);

// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}

@Override
public void onClick (View v)
{

float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";

// check if the fields are empty


if (TextUtils.isEmpty(Num1.getText().toString()) ||
TextUtils.isEmpty(Num2.getText().toString()))
return;

// read EditText and fill variables with numbers


num1 = Float.parseFloat(Num1.getText().toString());
num2 = Float.parseFloat(Num2.getText().toString());

// defines the button that has been clicked and performs the
corresponding operation
// write operation into oper, we will use it later for output
switch (v.getId())
{
case R.id.Add:
oper = "+";
result = num1 + num2;
break;
case R.id.Sub:
oper = "-";
result = num1 - num2;
break;
case R.id.Mul:
oper = "*";
result = num1 * num2;
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}

 So now the Coding part is also completed.


 Now run the application to see the output.

Output:
Result:
Thus a Simple Android Application for Native Calculator is developed and
executed successfully.

Ex.No: 4. Write an application that draws basic graphical primitives on the screen.

Aim:
To develop a Simple Android Application that draws basic Graphical Primitives
on the screen.

Procedure:
Creating a New project:

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

 Then type the Application name as “ex.no.4″ and click Next.


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

 Then select the Empty Activity and click Next.

 Finally click Finish.


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

Designing layout for the Android Application:

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


 Now click on Text as shown below.

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

Code for Activity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"

4 android:layout_height="match_parent">

6 <ImageView

7 android:layout_width="match_parent"

8 android:layout_height="match_parent"

9 android:id="@+id/imageView" />

10</RelativeLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Java Coding for the Android Application:

 Click on app -> java -> com.example.exno4 -> MainActivity.


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

Code for MainActivity.java:

1 package com.example.exno4;

3 import android.app.Activity;

4 import android.graphics.Bitmap;

5 import android.graphics.Canvas;

6 import android.graphics.Color;

7 import android.graphics.Paint;

8 import android.graphics.drawable.BitmapDrawable;

9 import android.os.Bundle;

10import android.widget.ImageView;

11
12public class MainActivity extends Activity

13{

14 @Override

15 public void onCreate(Bundle savedInstanceState)

16 {

17 super.onCreate(savedInstanceState);

18 setContentView(R.layout.activity_main);

19

20 //Creating a Bitmap

21 Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);

22

23 //Setting the Bitmap as background for the ImageView

24 ImageView i = (ImageView) findViewById(R.id.imageView);

25 i.setBackgroundDrawable(new BitmapDrawable(bg));

26

27 //Creating the Canvas Object

28 Canvas canvas = new Canvas(bg);

29

30 //Creating the Paint Object and set its color & TextSize

31 Paint paint = new Paint();

32 paint.setColor(Color.BLUE);

33 paint.setTextSize(50);

34
35 //To draw a Rectangle

36 canvas.drawText("Rectangle", 420, 150, paint);

37 canvas.drawRect(400, 200, 650, 700, paint);

38

39 //To draw a Circle

40 canvas.drawText("Circle", 120, 150, paint);

41 canvas.drawCircle(200, 350, 150, paint);

42

43 //To draw a Square

44 canvas.drawText("Square", 120, 800, paint);

45 canvas.drawRect(50, 850, 350, 1150, paint);

46

47 //To draw a Line

48 canvas.drawText("Line", 480, 800, paint);

49 canvas.drawLine(520, 850, 520, 1150, paint);

50 }

51}

 So now the Coding part is also completed.


 Now run the application to see the output.

Output:
Result:
Thus a Simple Android Application that draws basic Graphical Primitives on
the screen is developed and executed successfully.

5. Develop an application that makes use of database.


Ex.No: 5. Develop an application that makes use of database.

Aim:
To develop a Simple Android Application that makes use of Database.

Procedure:
Creating a New project:

 Open Android Studio and then click on File -> New -> New project.
 Then type the Application name as “ex.no.5″ and click Next.

 Then select the Minimum SDK as shown below and click Next.
 Then select the Empty Activity and click Next.

 Finally click Finish.


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

Designing layout for the Android Application:

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


 Now click on Text as shown below.

 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"?>


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="20dp"
android:text="Student Details"
android:textSize="30sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="110dp"
android:text="Enter Rollno:"
android:textSize="20sp" />

<EditText
android:id="@+id/Rollno"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="100dp"
android:inputType="number"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="160dp"
android:text="Enter Name:"
android:textSize="20sp" />
<EditText
android:id="@+id/Name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="150dp"
android:inputType="text"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="210dp"
android:text="Enter Marks:"
android:textSize="20sp" />

<EditText
android:id="@+id/Marks"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="175dp"
android:layout_y="200dp"
android:inputType="number"
android:textSize="20sp" />

<Button
android:id="@+id/Insert"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="300dp"
android:text="Insert"
android:textSize="30dp" />

<Button
android:id="@+id/Delete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="300dp"
android:text="Delete"
android:textSize="30dp" />

<Button
android:id="@+id/Update"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="400dp"
android:text="Update"
android:textSize="30dp" />

<Button
android:id="@+id/View"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="400dp"
android:text="View"
android:textSize="30dp" />

<Button
android:id="@+id/ViewAll"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="500dp"
android:text="View All"
android:textSize="30dp" />

</AbsoluteLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Java Coding for the Android Application:

 Click on app -> java -> com.example.exno5 -> MainActivity.


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

Code for MainActivity.java:

package com.example.exno5;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener


{
EditText Rollno,Name,Marks;
Button Insert,Delete,Update,View,ViewAll;
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Rollno=(EditText)findViewById(R.id.Rollno);
Name=(EditText)findViewById(R.id.Name);
Marks=(EditText)findViewById(R.id.Marks);
Insert=(Button)findViewById(R.id.Insert);
Delete=(Button)findViewById(R.id.Delete);
Update=(Button)findViewById(R.id.Update);
View=(Button)findViewById(R.id.View);
ViewAll=(Button)findViewById(R.id.ViewAll);

Insert.setOnClickListener(this);
Delete.setOnClickListener(this);
Update.setOnClickListener(this);
View.setOnClickListener(this);
ViewAll.setOnClickListener(this);

// Creating database and table


db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name
VARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
// Inserting a record to the Student table
if(view==Insert)
{
// Checking for empty fields
if(Rollno.getText().toString().trim().length()==0||
Name.getText().toString().trim().length()==0||
Marks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student
VALUES('"+Rollno.getText()+"','"+Name.getText()+
"','"+Marks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
// Deleting a record from the Student table
if(view==Delete)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE
rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Updating a record in the Student table
if(view==Update)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst()) {
db.execSQL("UPDATE student SET name='" + Name.getText() +
"',marks='" + Marks.getText() +
"' WHERE rollno='"+Rollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Rollno");
}
clearText();
}
// Display a record from the Student table
if(view==View)
{
// Checking for empty roll number
if(Rollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
rollno='"+Rollno.getText()+"'", null);
if(c.moveToFirst())
{
Name.setText(c.getString(1));
Marks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
// Displaying all the records
if(view==ViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
Rollno.setText("");
Name.setText("");
Marks.setText("");
Rollno.requestFocus();
}
}

 So now the Coding part is also completed.


 Now run the application to see the output.

Output:
Result:
Thus a Simple Android Application that makes use of Database is developed
and executed successfully.
Ex.No:6. Develop an application that makes use of RSS Feed.

Aim:
To develop a Android Application that makes use of RSS Feed.

Procedure:
Creating a New project:

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

 Then type the Application name as “ex.no.6″ and click Next.


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

 Then select the Empty Activity and click Next.


 Finally click Finish.

 It will take some time to build and load the project.


 After completion it will look as given below.
Designing layout for the Android Application:

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

 Now click on Text as shown below.


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

Code for Activity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3 android:layout_width="fill_parent"

4 android:layout_height="fill_parent"

5 android:orientation="vertical" >

7 <ListView

8 android:id="@+id/listView"

9 android:layout_width="match_parent"

10 android:layout_height="wrap_content" />

11
12</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Adding permissions in Manifest for the Android Application:

 Click on app -> manifests -> AndroidManifest.xml


 Now include the INTERNET permissions in the AndroidManifest.xml file as
shown below

Code for AndroidManifest.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

3 package="com.example.exno6" >

5 <uses-permission android:name="android.permission.INTERNET"/>

7 <application

8 android:allowBackup="true"

9 android:icon="@mipmap/ic_launcher"

10 android:label="@string/app_name"
11 android:supportsRtl="true"

12 android:theme="@style/AppTheme" >

13 <activity android:name=".MainActivity" >

14 <intent-filter>

15 <action android:name="android.intent.action.MAIN" />

16

17 <category android:name="android.intent.category.LAUNCHER" />

18 </intent-filter>

19 </activity>

20 </application>

21

22</manifest>

 So now the Permissions are added in the Manifest.

Java Coding for the Android Application:

 Click on app -> java -> com.example.exno6 -> MainActivity.


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

Code for MainActivity.java:

1 package com.example.exno6;

3 import android.app.ListActivity;

4 import android.content.Intent;

5 import android.net.Uri;

6 import android.os.AsyncTask;

7 import android.os.Bundle;

8 import android.view.View;

9 import android.widget.ArrayAdapter;

10 import android.widget.ListView;

11 import org.xmlpull.v1.XmlPullParser;
12 import org.xmlpull.v1.XmlPullParserException;

13 import org.xmlpull.v1.XmlPullParserFactory;

14 import java.io.IOException;

15 import java.io.InputStream;

16 import java.net.MalformedURLException;

17 import java.net.URL;

18 import java.util.ArrayList;

19 import java.util.List;

20

21 public class MainActivity extends ListActivity

22 {

23 List headlines;

24 List links;

25

26 @Override

27 protected void onCreate(Bundle savedInstanceState)

28 {

29 super.onCreate(savedInstanceState);

30 new MyAsyncTask().execute();

31 }

32

33 class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>

34 {
35 @Override

36 protected ArrayAdapter doInBackground(Object[] params)

37 {

38 headlines = new ArrayList();

39 links = new ArrayList();

40 try

41 {

42 URL url = new URL("http://www.codingconnect.net/feed");

43 XmlPullParserFactory factory =
XmlPullParserFactory.newInstance();
44
factory.setNamespaceAware(false);
45
XmlPullParser xpp = factory.newPullParser();
46

47
// We will get the XML from an input stream
48
xpp.setInput(getInputStream(url), "UTF_8");
49
boolean insideItem = false;
50

51
// Returns the type of current event: START_TAG, END_TAG,
52
etc..
53
int eventType = xpp.getEventType();

54
while (eventType != XmlPullParser.END_DOCUMENT)

55
{
56
if (eventType == XmlPullParser.START_TAG)

57
58 {

59 if (xpp.getName().equalsIgnoreCase("item"))

60 {

61 insideItem = true;

62 }

63 else if (xpp.getName().equalsIgnoreCase("title"))

64 {

65 if (insideItem)

66 headlines.add(xpp.nextText()); //extract
the headline
67
}
68
else if (xpp.getName().equalsIgnoreCase("link"))
69
{
70
if (insideItem)
71
links.add(xpp.nextText()); //extract the
72
link of article

73
}

74
}

75
else if(eventType==XmlPullParser.END_TAG &&
76 xpp.getName().equalsIgnoreCase("item"))

77 {

78 insideItem=false;

79 }

80 eventType = xpp.next(); //move to next element


81 }

82

83 }

84 catch (MalformedURLException e)

85 {

86 e.printStackTrace();

87 }

88 catch (XmlPullParserException e)

89 {

90 e.printStackTrace();

91 }

92 catch (IOException e)

93 {

94 e.printStackTrace();

95 }

96 return null;

97 }

98 protected void onPostExecute(ArrayAdapter adapter)

99 {

100 adapter = new ArrayAdapter(MainActivity.this,


android.R.layout.simple_list_item_1, headlines);
101
setListAdapter(adapter);
102
}
103
104 }

105

106 @Override

107 protected void onListItemClick(ListView l, View v, int position, long id)

108 {

109 Uri uri = Uri.parse((links.get(position)).toString());

110 Intent intent = new Intent(Intent.ACTION_VIEW, uri);

111 startActivity(intent);

112 }

113

114 public InputStream getInputStream(URL url)

115 {

116 try

117 {

118 return url.openConnection().getInputStream();

119 }

120 catch (IOException e)

return null;

}
 So now the Coding part is also completed.
 Now run the application to see the output.

Output:

Result:
Thus Android Application that makes use of RSS Feed is developed and
executed successfully.
Ex.9: Implement an application that writes data to the SD card.

Aim:
To develop a Android Application that writes data to the SD Card.

Procedure:
Creating a New project:

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

 Then type the Application name as “ex.no.9″ and click Next.


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

 Then select the Empty Activity and click Next.


 Finally click Finish.

 It will take some time to build and load the project.


 After completion it will look as given below.
Designing layout for the Android Application:

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

 Now click on Text as shown below.


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

Code for Activity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3 android:layout_width="match_parent"

4 android:layout_height="match_parent"

5 android:layout_margin="20dp"

6 android:orientation="vertical">

8 <EditText

9 android:id="@+id/editText"

10 android:layout_width="match_parent"

11 android:layout_height="wrap_content"
12 android:singleLine="true"

13 android:textSize="30dp" />

14

15 <Button

16 android:id="@+id/button"

17 android:layout_width="match_parent"

18 android:layout_height="wrap_content"

19 android:layout_margin="10dp"

20 android:text="Write Data"

21 android:textSize="30dp" />

22

23 <Button

24 android:id="@+id/button2"

25 android:layout_width="match_parent"

26 android:layout_height="wrap_content"

27 android:layout_margin="10dp"

28 android:text="Read data"

29 android:textSize="30dp" />

30

31 <Button

32 android:id="@+id/button3"

33 android:layout_width="match_parent"

34 android:layout_height="wrap_content"
35 android:layout_margin="10dp"

36 android:text="Clear"

37 android:textSize="30dp" />

38

39</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Adding permissions in Manifest for the Android Application:

 Click on app -> manifests -> AndroidManifest.xml


 Now include the WRITE_EXTERNAL_STORAGE permissions in the
AndroidManifest.xml file as shown below

Code for AndroidManifest.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.exno9" >

5 <uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
6

7
<application
8
android:allowBackup="true"
9
android:icon="@mipmap/ic_launcher"
10
android:label="@string/app_name"
11
android:supportsRtl="true"
12
android:theme="@style/AppTheme" >
13
<activity android:name=".MainActivity" >
14
<intent-filter>
15
<action android:name="android.intent.action.MAIN" />
16

17
<category android:name="android.intent.category.LAUNCHER" />
18
</intent-filter>
19
</activity>
20
</application>
21
</manifest>

 So now the Permissions are added in the Manifest.

Java Coding for the Android Application:


 Click on app -> java -> com.example.exno9 -> MainActivity.

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

Code for MainActivity.java:

1 package com.example.exno9;

3 import android.os.Bundle;

4 import android.support.v7.app.AppCompatActivity;

5 import android.view.View;

6 import android.widget.Button;

7 import android.widget.EditText;

8 import android.widget.Toast;

1 import java.io.BufferedReader;
0 import java.io.File;

1 import java.io.FileInputStream;
1
import java.io.FileOutputStream;
1
import java.io.InputStreamReader;
2

1
3 public class MainActivity extends AppCompatActivity

1{
4
EditText e1;
1
Button write,read,clear;
5
@Override
1
6 protected void onCreate(Bundle savedInstanceState)

1 {
7
super.onCreate(savedInstanceState);
1
8 setContentView(R.layout.activity_main);

1
9
e1= (EditText) findViewById(R.id.editText);

2
write= (Button) findViewById(R.id.button);
0
read= (Button) findViewById(R.id.button2);
2
1 clear= (Button) findViewById(R.id.button3);

2
2
write.setOnClickListener(new View.OnClickListener()

2
{
3
@Override
2
4 public void onClick(View v)

2 {
5
String message=e1.getText().toString();
2
try
6
{
2
7 File f=new File("/sdcard/myfile.txt");

2 f.createNewFile();
8
FileOutputStream fout=new FileOutputStream(f);
2
fout.write(message.getBytes());
9
fout.close();
3
0 Toast.makeText(getBaseContext(),"Data Written in
SDCARD",Toast.LENGTH_LONG).show();
3
1 }

3 catch (Exception e)
2
{
3
3 Toast.makeText(getBaseContext(),e.getMessage(),Toast.LEN
GTH_LONG).show();
3
4 }

3 }

5
});

3
6
read.setOnClickListener(new View.OnClickListener()
3
7 {

3
8 @Override

3 public void onClick(View v)


9
{
4
String message;
0
String buf = "";
4
1 try

4 {
2
File f = new File("/sdcard/myfile.txt");
4
FileInputStream fin = new FileInputStream(f);
3
BufferedReader br = new BufferedReader(new
4
InputStreamReader(fin));
4
while ((message = br.readLine()) != null)
4
5 {

4 buf += message;
6
}
4
7 e1.setText(buf);

4 br.close();

8
fin.close();

4
Toast.makeText(getBaseContext(),"Data Recived from
9
SDCARD",Toast.LENGTH_LONG).show();

5
}
0
catch (Exception e)
5
1 {

5 Toast.makeText(getBaseContext(), e.getMessage(),
2 Toast.LENGTH_LONG).show();

5 }
3
}
5
});
4

5
5 clear.setOnClickListener(new View.OnClickListener()

5 {
6
@Override
5
public void onClick(View v)
7
{
5
8 e1.setText("");

5 }
9
});
6
0 }

6}
1

6
2

6
3

6
4

6
5

6
6

6
7

6
8

6
9

7
0

7
1

7
2

7
3

7
4

7
5

7
6

7
7

7
8

7
9

8
0

8
1

8
2

8
3

8
4

8
5

8
6

8
7

8
8

8
9

9
0

 So now the Coding part is also completed.


 Now run the application to see the output.

Output:
Result:
Thus Android Application that writes data to the SD Card is developed and
executed successfully.

Ex.No: 10. Implement an application that creates an alert upon receiving a message.

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.
 Then type the Application name as “ex.no.10″ and click Next.

 Then select the Minimum SDK as shown below and click Next.
 Then select the Empty Activity and click Next.

 Finally click Finish.


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

Creating Second Activity for the Android Application:

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


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

 Thus Second Activity For the application is created.

Designing layout for the Android Application:

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


 Now click on Text as shown below.

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

Code for Activity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"

4 android:layout_height="match_parent"

5 android:layout_margin="10dp"

6 android:orientation="vertical">

8 <TextView

9 android:layout_width="wrap_content"

10 android:layout_height="wrap_content"

11 android:text="Message"

12 android:textSize="30sp" />

13

14 <EditText

15 android:id="@+id/editText"

16 android:layout_width="match_parent"

17 android:layout_height="wrap_content"

18 android:singleLine="true"

19 android:textSize="30sp" />

20

21 <Button

22 android:id="@+id/button"

23 android:layout_width="wrap_content"

24 android:layout_height="wrap_content"

25 android:layout_margin="30dp"
26 android:layout_gravity="center"

27 android:text="Notify"

28 android:textSize="30sp"/>

29

30</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Java Coding for the Android Application:

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


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

Code for MainActivity.java:

1 package com.example.exno10;

3 import android.app.Notification;

4 import android.app.NotificationManager;

5 import android.app.PendingIntent;

6 import android.content.Intent;

7 import android.os.Bundle;

8 import android.support.v7.app.AppCompatActivity;

9 import android.view.View;

1 import android.widget.Button;
0
import android.widget.EditText;
1
1

1 public class MainActivity extends AppCompatActivity


2
{
1
Button notify;
3
EditText e;
1
4 @Override

1 protected void onCreate(Bundle savedInstanceState)


5
{
1
super.onCreate(savedInstanceState);
6
setContentView(R.layout.activity_main);
1
7

1 notify= (Button) findViewById(R.id.button);


8
e= (EditText) findViewById(R.id.editText);
1
9

2 notify.setOnClickListener(new View.OnClickListener()

0
{

2
@Override
1
public void onClick(View v)
2
2 {

2 Intent intent = new Intent(MainActivity.this,

3 SecondActivity.class);

2 PendingIntent pending =

4 PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

2 Notification noti = new


5 Notification.Builder(MainActivity.this).setContentTitle("New
Message").setContentText(e.getText().toString()).setSmallIcon(R.mipmap.ic_la
2
uncher).setContentIntent(pending).build();
6
NotificationManager manager = (NotificationManager)
2
getSystemService(NOTIFICATION_SERVICE);
7
noti.flags |= Notification.FLAG_AUTO_CANCEL;
2
8 manager.notify(0, noti);

2 }
9
});
3
}
0
}
3
1

3
2

3
3

3
4

3
5

3
6

3
7

3
8

3
9

4
0

 So now the Coding part is also completed.


 Now run the application to see the output.

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

Ex.No:11. Write a mobile application that creates alarm clock

Aim:
To develop a Android Application that creates Alarm Clock.

Procedure:
Creating a New project:

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

 Then type the Application name as “ex.no.11″ and click Next.


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

 Then select the Empty Activity and click Next.


 Finally click Finish.

 It will take some time to build and load the project.


 After completion it will look as given below.
Creating Second Activity for the Android Application:

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

 Type the Activity Name as AlarmReceiver and click Finish button.


 Thus Second Activity For the application is created.

Designing layout for the Android Application:

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

 Now click on Text as shown below.


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

Code for Activity_main.xml:

1 <?xml version="1.0" encoding="utf-8"?>

2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

3 android:layout_width="match_parent"

4 android:layout_height="match_parent"

5 android:orientation="vertical">

7 <TimePicker

8 android:id="@+id/timePicker"

9 android:layout_width="wrap_content"

10 android:layout_height="wrap_content"

11 android:layout_gravity="center" />
12

13 <ToggleButton

14 android:id="@+id/toggleButton"

15 android:layout_width="wrap_content"

16 android:layout_height="wrap_content"

17 android:layout_gravity="center"

18 android:layout_margin="20dp"

19 android:checked="false"

20 android:onClick="OnToggleClicked" />

21

22</LinearLayout>

 Now click on Design and your application will look as given below.

 So now the designing part is completed.

Changes in Manifest for the Android Application:


 Click on app -> manifests -> AndroidManifest.xml

 Now change the activity tag to receiver tag in the AndroidManifest.xml file as
shown below

Code for AndroidManifest.xml:

1 <?xml version="1.0" encoding="utf-8"?>


2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

3 package="com.example.exno11" >

5 <application

6 android:allowBackup="true"

7 android:icon="@mipmap/ic_launcher"

8 android:label="@string/app_name"

9 android:supportsRtl="true"

10 android:theme="@style/AppTheme" >

11 <activity android:name=".MainActivity" >

12 <intent-filter>

13 <action android:name="android.intent.action.MAIN" />

14

15 <category android:name="android.intent.category.LAUNCHER" />

16 </intent-filter>

17 </activity>

18 <receiver android:name=".AlarmReceiver" >

19 </receiver>

20 </application>

21

22</manifest>

 So now the changes are done in the Manifest.


Java Coding for the Android Application:

Java Coding for Main Activity:

 Click on app -> java -> com.example.exno11 -> MainActivity.

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

Code for MainActivity.java:

1 package com.example.exno11;

3 import android.app.AlarmManager;

4 import android.app.PendingIntent;

5 import android.content.Intent;

6 import android.os.Bundle;

7 import android.support.v7.app.AppCompatActivity;
8 import android.view.View;

9 import android.widget.TimePicker;

10import android.widget.Toast;

11import android.widget.ToggleButton;

12

13import java.util.Calendar;

14

15public class MainActivity extends AppCompatActivity

16{

17 TimePicker alarmTimePicker;

18 PendingIntent pendingIntent;

19 AlarmManager alarmManager;

20

21 @Override

22 protected void onCreate(Bundle savedInstanceState)

23 {

24 super.onCreate(savedInstanceState);

25 setContentView(R.layout.activity_main);

26 alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);

27 alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

28 }

29 public void OnToggleClicked(View view)

30 {
31 long time;

32 if (((ToggleButton) view).isChecked())

33 {

34 Toast.makeText(MainActivity.this, "ALARM ON",


Toast.LENGTH_SHORT).show();
35
Calendar calendar = Calendar.getInstance();
36
calendar.set(Calendar.HOUR_OF_DAY,
37
alarmTimePicker.getCurrentHour());

38
calendar.set(Calendar.MINUTE,
39alarmTimePicker.getCurrentMinute());

40 Intent intent = new Intent(this, AlarmReceiver.class);

41 pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

42

43 time=(calendar.getTimeInMillis()-
(calendar.getTimeInMillis()%60000));
44
if(System.currentTimeMillis()>time)
45
{
46
if (calendar.AM_PM == 0)
47
time = time + (1000*60*60*12);
48
else
49
time = time + (1000*60*60*24);
50
}
51
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000,
52
pendingIntent);

53
54 }

55 else

56 {

57 alarmManager.cancel(pendingIntent);

Toast.makeText(MainActivity.this, "ALARM OFF",


Toast.LENGTH_SHORT).show();

 So now the Coding part of Main Activity is completed.

Java Coding for Alarm Receiver:

 Click on app -> java -> com.example.exno11 -> AlarmReceiver.

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

Code for AlarmReceiver.java:


?

1 package com.example.exno11;

3 import android.content.BroadcastReceiver;

4 import android.content.Context;

5 import android.content.Intent;

6 import android.media.Ringtone;

7 import android.media.RingtoneManager;

8 import android.net.Uri;

9 import android.widget.Toast;

10

11public class AlarmReceiver extends BroadcastReceiver

12{

13 @Override

14 public void onReceive(Context context, Intent intent)

15 {

16 Toast.makeText(context, "Alarm! Wake up! Wake up!",


Toast.LENGTH_LONG).show();
17
Uri alarmUri =
18
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
19
if (alarmUri == null)
20
{
21
alarmUri =
22RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
23 }

24 Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);

25 ringtone.play();

 So now the Coding part of Alarm Receiver is also completed.


 Now run the application to see the output.

Output:
Result:
Thus Android Application that creates Alarm Clock is developed and
executed successfully.

You might also like