The document describes a lab manual for a mobile application development course. It includes objectives of the course such as understanding mobile app development frameworks and designing mobile apps. It lists 12 experiments related to developing mobile apps using various features. The experiments include developing apps using GUI components, layout managers, database, GPS etc. It also lists the outcomes of the course and equipment required.
2. BuildersEngineeringCollege
CS6611- MOBILE APPLICATION DEVELOPMENT LABORATORY L T P C
0 0 3 2
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.
3. BuildersEngineeringCollege
1
Ex.No: 1 Develop an application that uses GUI components, Font and Colours
Date:
Aim:
To develop an android application that uses GUI components, Font and Colours
using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?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"
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="helloapp.example.com.demoapp.MainActivity"
android:background="#03acda"
android:clickable="false"
android:id="@+id/canvas">
<TextView
9. BuildersEngineeringCollege
7
Ex.No: 2 Develop an application that uses Layout Managers and event listeners
Date:
Aim:
To develop an android application that uses Layout Managers and event listeners
using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Layout and Listener Program"
15. BuildersEngineeringCollege
13
Ex.No: 3 Develop a native calculator application
Date:
Aim:
To develop a native calculator application using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="77dp"
23. BuildersEngineeringCollege
21
Ex.No: 4 Write an application that draws basic graphical
Date: primitives on the screen
Aim:
To write an application that draws basic graphical primitives on the screen using
android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java and
create samplecanvas.java files and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns: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:fitsSystemWindows="true"
tools:context="com.sg.graphic.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
27. BuildersEngineeringCollege
25
Ex.No: 5 Develop an application that makes use of database
Date:
Aim:
To develop an application that makes use of database using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="@string/roll_no"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editRollno"
android:inputType="number"
33. BuildersEngineeringCollege
31
Ex.No: 6 Develop an application that makes use of RSS Feed
Date:
Aim:
To develop an application that makes use of RSS Feed using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code also create supporting layout files.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code also create the supporting java codes.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?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"
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="com.linuxpert.mitcourses.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstMain" />
</RelativeLayout>
41. BuildersEngineeringCollege
39
progressBar = (ProgressBar) findViewById(R.id.feedProgress);
Intent i = getIntent();
//gets the root intent
String title = i.getStringExtra("title");
//gets the String supplied with the tag title
String url = i.getStringExtra("link");
//gets the String supplied with the tag link
getRssData getRssData = new getRssData();
getRssData.execute(url);
//actionbar
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private class getRssData extends AsyncTask<String,Void,ArrayList<RssItem>>{
@Override
protected ArrayList<RssItem> doInBackground(String... params) {
try {
RssReader rssReader = new RssReader(params[0]);
//create rss reader
return rssReader.getItems();
//returns items parsed from rss file
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<RssItem> rssItems) {
if(rssItems!=null) {
ListView rssList = (ListView) findViewById(R.id.lstRssFeed);
RssListAdapter adapter = new RssListAdapter(local, R.layout.rss_list_layout,
rssItems);
rssList.setAdapter(adapter);
rssList.setOnItemClickListener(new RssItemClickListener(local,rssItems));
Toast.makeText(local, "Feed Loaded", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(local,"Could not load feed. Check your network
connections.",Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
42. BuildersEngineeringCollege
40
}
@Override
protected void onPreExecute() {
Toast.makeText(local,"Loading Feed",Toast.LENGTH_LONG).show();
}
}
}
RssParseHandler.java
package com.sg.mitcourses;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
public class RssParseHandler extends DefaultHandler {
private ArrayList<RssItem> rssItems; //list to store rss items parsed
private RssItem currentItem; //stores the currently parsing RssItem
private boolean parsingTitle, parsingLink; //parsing title/link indicators
public RssParseHandler() {
rssItems = new ArrayList<>();
}
public ArrayList<RssItem> getRssItems() {
return rssItems;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes
attributes) throws SAXException {
if("item".equals(qName)){
currentItem = new RssItem(); //create a new RssItem to store data
} else if ("title".equals(qName)){
parsingTitle = true;
}
else if ("link".equals(qName)){
parsingLink = true;
}
//sets the current item to rss item when parsing item
//sets appropriate indicators to true when opening tags are processed
}
43. BuildersEngineeringCollege
41
@Override
public void endElement(String uri, String localName, String qName) throws
SAXException {
if("item".equals(qName)){
rssItems.add(currentItem);
currentItem = null;
} else if ("title".equals(qName)){
parsingTitle = false;
}
else if ("link".equals(qName)){
parsingLink = false;
}
//adds the current item to list
//sets appropriate indicators to false when closing tags are processed
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(parsingTitle){
if(currentItem!=null) {
currentItem.setTitle(new String(ch, start, length)); //sets title of the current item
}
} else if (parsingLink){
if (currentItem!=null){
currentItem.setLink(new String(ch, start, length)); //sets link of the current item
}
}
/*
* Here we used if(parsingTitle), if(parsingLink).
* These indicators indicate whether title or link is executed.
* It helps to avoid the handler from parsing other unwanted characters inside other rss
tags.
*/
}
}
RssReader.java
package com.sg.mitcourses;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class RssReader {
private String rssUrl;
44. BuildersEngineeringCollege
42
public RssReader(String rssUrl) {
this.rssUrl = rssUrl;
}
public ArrayList<RssItem> getItems() throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
RssParseHandler handler = new RssParseHandler();
parser.parse(rssUrl,handler);
//parse the rss file located at rssUrl using the parsing handler 'handler'.
return handler.getRssItems();
//returns the list of items parsed
}
}
Course.java
package com.blogspot.shuttereditz.mitcourses;
public class Course {
private String title,url;
public Course(String title, String url) {
this.title = title;
this.url = url;
}
public String getTitle() {
return title;
}
public String getUrl() {
return url;
}
}
46. BuildersEngineeringCollege
44
Ex.No: 7 Implement an application that implements Multithreading
Date:
Aim:
To implement an application that implements Multithreading using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.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:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="4dip" >
</ProgressBar>
<TextView
48. BuildersEngineeringCollege
46
}
}
};
new Thread(runnable).start();
}
// Simulating something timeconsuming
private void doFakeWork() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output:
Result:
Thus the application that implements Multithreading is implemented and tested
using android studio.
49. BuildersEngineeringCollege
47
Ex.No: 8 Develop a native application that uses GPS location information
Date:
Aim:
To develop a native application that uses GPS location information using android
studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/show_Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show_Location"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
50. BuildersEngineeringCollege
48
Mainactivity.java
package com.sg.gps;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
public class GPStrace extends Service implements
LocationListener{
private final Context context;
boolean isGPSEnabled=false;
boolean canGetLocation=false;
boolean isNetworkEnabled=false;
Location location;
double latitude;
double longtitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
private static final long MIN_TIME_BW_UPDATES=1000*60*1;
protected LocationManager locationManager;
public GPStrace(Context context)
{
this.context=context;
getLocation();
}
public Location getLocation()
{
try{
locationManager=(LocationManager)
context.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locationManager.isProviderEnabled(LocationManag
er.GPS_PROVIDER);
application that draws
basic graphical primitives
on the screen in android
Develop
a native
calculator
application
Implement an application
that creates an alert
upon receiving a
message in android
51. BuildersEngineeringCollege
49
Develop
an
application that makes
use of RSS Feed
isNetworkEnabled=locationManager.isProviderEnabled(LocationM
anager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled){
}else{
this.canGetLocation=true;
if(isNetworkEnabled){
Develop
an
application that makes
use of database
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
}
if(locationManager!=null){
Write a
mobile
application that creates
alarm clock in android
location=locationManager.getLastKnownLocation(LocationManager
.NETWORK_PROVIDER);
if(location !=null){
latitude=location.getLatitude();
longtitude=location.getLongitude();
FAMOUS QUOTES
}
}
}
if(isGPSEnabled){
if(location==null){
locationManager.requestLocationUpdates(LocationManager.GPS_PR
OVIDER,MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
if(locationManager!=null){
location=locationManager.getLastKnownLocation(LocationManager
.GPS_PROVIDER);
if(location!=null){
latitude=location.getLatitude();
longtitude=location.getLongitude();
}
Androidmaifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sg.gps">
<application
53. BuildersEngineeringCollege
51
Ex.No: 9 Implement an application that writes data to the SD card
Date:
Aim:
To implement an application that writes data to the SD card using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?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"
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="com.linuxpert.myfileapp.MainActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtWrite"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
55. BuildersEngineeringCollege
53
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtWrite = (EditText)findViewById(R.id.txtWrite);
txtRead = (TextView)findViewById(R.id.txtRead);
btnWrite = (Button)findViewById(R.id.btnWrite);
btnRead = (Button)findViewById(R.id.btnRead);
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = txtWrite.getText().toString();//read data from text field
try {
FileOutputStream ofStream = new FileOutputStream(new
File(getFilesDir(),"demo"));
/*
* file output stream which writes into a new file named demo with the path we
get from
* getFilesDir() function which corresponds to internal memory.
* - getCacheDir() - cache memory
*/
OutputStreamWriter osWriter = new OutputStreamWriter(ofStream);
//create a new writer from ofStream
osWriter.write(text);
//writes data into the file
osWriter.close();
ofStream.close();
//closing stream and writer
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream ifStream = new FileInputStream(new File(getFilesDir(),"demo"));
/* creates input stream for reading from the file*/
BufferedReader bReader = new BufferedReader(new
InputStreamReader(ifStream));
/* Here we created a new InputStreamReader from the ifStream.
* (Like we created an OutputStreamWriter from ofStream).
* And we've created a BufferedReader from this InputStreamReader.
* The buffered reader is used to read data from a file as Strings.
*/
String read=""; //creating an empty string to save the read data from file.
String line;
/* Here we read the file line by line so String line is used to store the data
* of the current line the reader pointer is positioned at.
*/
while((line=bReader.readLine())!=null){
/*confirms the line is not null so as to execute loop.*/
56. BuildersEngineeringCollege
54
/*If it is null it means the pointer is at the end of file*/
read+=line+"n";
}
bReader.close();
ifStream.close();
//closing stream and reader
txtRead.setText(read); //setting the text of the text view
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Output:
Result:
Thus the application that writes data to the SD card is implemented and tested using
android studio.
57. BuildersEngineeringCollege
55
Ex.No: 10 Implement an application that creates an alert upon
Date: receiving a message
Aim:
To implement an application that creates an alert upon receiving a message using
android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<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=".MainActivity">
<TextView
android:id="@+id/simpleDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert Dialog" />
<Button
58. BuildersEngineeringCollege
56
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:layout_below="@+id/simpleDialog"
android:layout_marginTop="16dp"
android:onClick="showDialog"/>
</RelativeLayout>
Mainactivity.java
package com.sg.alertdialog;
// Simple android alert dialog example
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDialog(View view){
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Welcome to EBETi");
// Alert dialog button
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Alert dialog action goes here
// onClick button code here
dialog.dismiss();// use dismiss to cancel alert dialog
}
});
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
59. BuildersEngineeringCollege
57
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Output:
Result:
Thus the application that creates an alert upon receiving a message is implemented
and tested using android studio.
60. BuildersEngineeringCollege
58
Ex.No: 11 Write a mobile application that creates alarm clock
Date:
Aim:
To write a mobile application that creates alarm clock using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/alarmTimePicker"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
63. BuildersEngineeringCollege
61
public void onReceive(final Context context, Intent intent) {
//this will update the UI with message
AlarmActivity inst = AlarmActivity.instance();
inst.setAlarmText("Alarm! Wake up! Wake up!");
//this will sound the alarm tone
//this will sound the alarm once, if you wish to
//raise alarm in loop continuously then use MediaPlayer and setLooping(true)
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Alarmservice.java
package com.sg.alarm;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
sendNotification("Wake Up! Wake Up!");
}
private void sendNotification(String msg) {
Log.d("AlarmService", "Preparing to send notification...: " + msg);
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
64. BuildersEngineeringCollege
62
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AlarmActivity.class), 0);
NotificationCompat.Builder alamNotificationBuilder = new
NotificationCompat.Builder(
this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alamNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
}
}
Output:
Result:
Thus the application alarm clock is developed and tested using android studio.