Application Developmen Lab
Application Developmen Lab
LAB MANUAL
191CSC611L
APPLICATION DEVELOPMENT LABORATORY (MOBILE/WEB)
OBJECTIVES:
To understand the components and structure of mobile application development frameworks for
Android and windows OS based mobiles.
To understand how to work with various mobile application development frameworks.
To learn the basic and important design concepts and issues of development of mobile applications.
To understand the capabilities and limitations of mobile devices.
LIST OF EXPERIMENTS:
1. Develop an application that uses GUI components, Layout Managers and event listeners.
2. Develop an application to simulate a keyboard.
3. Create an application that uses graphical primitives.
4. Develop an application that makes use of databases.
5. Implement an application that uses Multi-threading.
6. Develop a native application that uses GPS location information.
7. Implement an application that writes data to the SD card.
8. Implement an application that send a SMS and creates an alert upon receiving the SMS.
9. Create an application that makes use of Menu.
10. Develop an application to build an alarm clock.
11. Implement a hybrid mobile application for displaying a website.
12. Mini Project (Food delivery app, Attendance tracking app, Online ticket booking app etc.)
TOTAL: 60 PERIODS
OUTCOMES:
o Design mobile applications using GUI and Layouts.
o Develop mobile applications using Event listener and Databases
o Create mobile applications using RSS Feed and Multithreading
o Develop mobile applications using Internal/External Storage, SMS and GPS
o Build hybrid mobile applications
LIST OF EQUIPMENT FOR A BATCH OF 30 STUDENTS:
SOFTWARE: C / C++ / Java or equivalent compiler GnuPG, Snort, N-Stalker or Equivalent
HARDWARE: Standalone desktops – 30 Nos. (or) Server supporting 30 terminals or more.
REFERENCES:
MINI PROJECTS
COURSE/ : B.E Computer science and Total number of hours given in the
BRANCH Engineering syllabus
SUBJECT CODE : 191CSC611 L Lecture : -
SUBJECT TITLE : Application Development Tutorials : -
laboratory
YEAR/SEMESTER : III/6 Practical : 60
COURSE OBJECTIVES:
To understand the components and structure of mobile application development frameworks for
Android and windows OS based mobiles.
To understand how to work with various mobile application development frameworks.
To learn the basic and important design concepts and issues of development of mobile
applications.
To understand the capabilities and limitations of mobile devices
S.N LIST OF EXPERIMENTS RELEVANCE CO’S
O
1 Develop an application that uses GUI components, Layout CO1, CO2
Managers and event listeners.
2 Develop an application to simulate a keyboard. CO1, CO2
3 Create an application that uses graphical primitives. CO1,CO3
4 Develop an application that makes use of databases. CO1,CO3
5 Implement an application that uses Multi-threading. CO1,CO3
6 Develop a native application that uses GPS location CO1, CO2, CO4
information.
7 Implement an application that writes data to the SD card. CO1, CO4
8 Implement an application that send a SMS and creates an alert CO1, CO2, CO4
upon receiving the SMS.
9 Create an application that makes use of Menu. CO1, CO2, CO3
10 Develop an application to build an alarm clock. CO1, CO4
11 Implement a hybrid mobile application for displaying a CO1, CO2, CO4
website.
12 Mini Project (Food delivery app, Attendance tracking app, CO5
Online ticket booking app etc.)
Course Outcomes:
o Develop mobile applications using GUI and Layouts.
o Develop mobile applications using Event Listener.
o Develop mobile applications using Databases.
o Develop mobile applications using RSS Feed, Internal/External Storage, SMS, Multithreading and GPS.
o Analyze and discover own mobile app for simple needs.
Program Outcomes:
PO1 : Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
PO2 : Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
PO3 : Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental considerations.
PO4 : Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
PO5 : Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
PO6 :The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
PO7: Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable development.
PO8 : Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
PO9 : Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10 : Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
PO11: Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
PO12 : Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change
EX.NO. 1 DEVELOP AN APPLICATION THAT USES GUI COMPONENTS, LAYOUT
MANAGERS AND EVENT LISTENERS.
AIM:
To develop an application that uses GUI components, layout managers and event listeners.
PROCEDURE:
1) Open android studio and select new android project
2) Give project name and package name.
3) UI design for android application is done using a layout xml file. Android studio offers a layout editor
which allows you to drag and drop UI elements into the interface.
4) Create the activity_main.xml file and MainActivity.java file
5) Run the application
package com.example.font1;
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.button); 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;
Event Listeners
Two most commonly used event listeners – onClickEventListener() and
onLongClickEventListener(). Step 1:
First we need to define some variables for each items in the UI.
Button clickBtn, longClickBtn, allBtn, btnShow;
TextView sample;
EditText nameTxt;
Step 2:
Assign the UI elements to these variables using findViewById()
clickBtn = (Button) findViewById(R.id.btnClick);
longClickBtn = (Button) findViewById(R.id.btnLongClick);
allBtn = (Button) findViewById(R.id.btnAll);
btnShow = (Button) findViewById(R.id.btnShowName);
sample = (TextView) findViewById(R.id.txtSample);
nameTxt = (EditText) findViewById(R.id.txtName);
Step 3:
Implementing the onClick Listener:
/*Simple Click onClick Listener*/
clickBtn.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hai John!", Toast.LENGTH_SHORT).show();
}
});
This event listener toasts a message Hai John when the “Click Me!” button is clicked
Step 4:
Implementing the onLongClick Listener
/*Implement Long Click Listener*/
longClickBtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v)
{ Toast.makeText(getApplicationContext(), "Hai
there!", Toast.LENGTH_SHORT).show();
return false;
}
});
This
event listener toasts a message “Hai there!” when the “Long Click Me!” button is clicked and held.
Step 5:
Implementing onClick and onLongClick Event on the same button.
allBtn.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v)
{ Toast.makeText(getApplicationContext(), "You Just Clicked
Me!", Toast.LENGTH_SHORT).show();
}
});
allBtn.setOnLongClickListener(new View.OnLongClickListener()
{ @Override
public boolean onLongClick(View v)
{ Toast.makeText(getApplicationContext(), "You clicked me for so
long!", Toast.LENGTH_SHORT).show();
return false;
}
});
The button defined by the variable allBtn will toast two different messages when clicked
and longclicked
i.e, “You just Clicked Me!” when clicked and “You clicked me for so long!”
when long clicked.
Step 6:
Reading a data from a text field and writing it to a text view using event listeners.
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sample.setText(nameTxt.getText().toString());
}
});
This event reads the value of the nameTxt text field and writes it to sample TextView.
RESULT:
Thus the program to create a Mobile App to display GUI components anddifferent layouts is executed
successfully.
EX.NO.3 DEVELOP AN APPLICATION THAT DRAWS BASIC
GRAPHICAL PRIMITIVES
AIM:
To develop an application that draws basic graphical primitives on the screen in android.
PROCEDURE:
activity_main.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="com.example.ex_no_7.MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
tools:ignore="ContentDescription" />
</RelativeLayout>
MainActivity.java:
public package com.example.ex_no_3;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
@SuppressLint("ClickableViewAccessibility")
public class MainActivity extends ActionBarActivity implements
OnTouchListener{
ImageView iv;
Bitmap b;
Canvas c;
Paint p;
float dx=0,dy=0,ux=0,uy=0;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)this.findViewById(R.id.imageView1);
Display d = getWindowManager().getDefaultDisplay();
float dw = d.getWidth();
float dh = d.getHeight();
b = Bitmap.createBitmap((int) dw, (int)
dh,Bitmap.Config.ARGB_8888);
c = new Canvas(b);
p = new Paint();
p.setColor(Color.BLUE);
iv.setImageBitmap(b);
iv.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
dx =event.getX();
dy= event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
ux = event.getX();
uy = event.getY();
c.drawLine(dx, dy, ux, uy, p);
iv.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
}
return true;
}
RESULT: Thus a Mobile App has been developed to display all the graphics primitives and seen in the
Android Emulator.
EX NO: 4 DEVELOP AN APPLICATION THAT MAKES USE OF
DATABASE
AIM:
PROCEDURE:
We will use the SQLiteDatabase and SQLiteOpenHelper classes to implement database handling.
Creating database handler class
To handle a database we need to create a database handler class.
So, right click on the package directory (inside java folder) and select New then Java Class
Declare some static variables that will save the database and the table details we are going to create.
activity_main.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="com.example.ex_no_4.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Reg. No."
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginTop="20dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="26dp"
android:text="Marks"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText2"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
tools:ignore="TextFields" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="32dp"
android:text="ADD"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:text="VIEW ALL"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/editText3"
android:layout_marginLeft="24dp"
android:text="VIEW"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginLeft="27dp"
android:layout_marginTop="18dp"
android:text="UPDATE"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/button4"
android:text="DELETE"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java:
package com.example.ex_no_4;
import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity {
EditText name, regno, mark;
Button btnAdd, btnDelete, btnUpdate, btnView, btnViewAll;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
regno = (EditText) findViewById(R.id.editText1);
name = (EditText) findViewById(R.id.editText2);
mark = (EditText) findViewById(R.id.editText3);
btnAdd = (Button) findViewById(R.id.button1);
btnView = (Button) findViewById(R.id.button2);
btnViewAll = (Button) findViewById(R.id.button3);
btnUpdate = (Button) findViewById(R.id.button4);
btnDelete = (Button) findViewById(R.id.button5);
db = openOrCreateDatabase("Students", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(regno VARCHAR,name
VARCHR,mark VARCHAR);");
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (regno.getText().toString().trim().length() == 0||
name.getText().toString().trim().length() == 0 || mark.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('" + regno.getText() + "','" +
name.getText() + "','" + mark.getText() + "');");
showMessage("Success", "Record added");
clearText();
}
});
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (regno.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter Reg. No.");
return;
}
Cursor c = db.rawQuery("SELECT * FROM student WHERE regno='" +
regno.getText() + "'", null);
if (c.moveToFirst()) {
db.execSQL("DELETE FROM student WHERE regno='" +
regno.getText() + "'");
showMessage("Success", "Record Deleted");
}
else {
showMessage("Error", "Invalid Reg. No.");
}
clearText();
}
});
btnUpdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (regno.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter Reg. No.");
return;
}
Cursor c = db.rawQuery("SELECT * FROM student WHERE regno='" +
regno.getText() + "'", null);
if (c.moveToFirst()) {
db.execSQL("UPDATE student SET name='" + name.getText() +
"',mark='" + mark.getText() + "' WHERE regno='" + regno.getText() + "'");
showMessage("Success", "Record Modified");
}
else {
showMessage("Error", "Invalid Reg. No.");
}
clearText();
}
});
btnView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (regno.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter Reg. No.");
return;
}
Cursor c = db.rawQuery("SELECT * FROM student WHERE regno='" +
regno.getText() + "'", null);
if (c.moveToFirst()) {
name.setText(c.getString(1));
mark.setText(c.getString(2));
}
else {
showMessage("Error", "Invalid Reg. No.");
clearText();
}
}
});
btnViewAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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("Reg. No : " + c.getString(0) + "\n");
buffer.append("Name : " + c.getString(1) + "\n");
buffer.append("Mark : " + 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() {
regno.setText("");
name.setText("");
mark.setText("");
regno.requestFocus();
}
}
RESULT:
Thus a Mobile App is developing to display details from a database
EX NO: 5 IMPLEMENT AN APPLICATION THAT IMPLEMENTS
MULTI THREADING
AIM:
PROCEDURE:
activity_main.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="com.example.ex_no_9.MainActivity" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Start Progress"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java
package com.example.ex_no_5;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar p = (ProgressBar) findViewById(R.id.progressBar1);
final TextView t = (TextView) findViewById(R.id.textView1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i <= 100; i++) {
final int temp = i;
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
p.setProgress(temp);
t.setText(temp + "%");
}
});
}
}
};
new Thread(r).start();
}
});
}
}
RESULT:
Thus the application that implement multithreading executed successfull
EX NO:6 DEVELOP A NATIVE APPLICATION THAT USES GPS
LOCATION INFORMATION.
AIM:
PROCEDURE:
activity_main.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="com.example.ex_no_5.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="114dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="51dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:text="Current Location"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java:
package com.example.ex_no_6;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements LocationListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria c=new Criteria();
String s=lm.getBestProvider(c, false);
if(s!=null && !s.equals("")){
Location l=lm.getLastKnownLocation(s);
lm.requestLocationUpdates(s, 20000, 1, this);
if(l!=null)
onLocationChanged(l);
else
Toast.makeText(getApplicationContext(), "Location can't
be
retrieved !!!", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Provider not found !!!",
Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
TextView t1=(TextView)findViewById(R.id.textView1);
t1.setText("Latitude : \n"+arg0.getLatitude());
TextView t2=(TextView)findViewById(R.id.textView2);
t2.setText("Longitude : \n"+arg0.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
PROCEDURE:
activity_main.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="com.example.ex_no_6.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Path"
tools:ignore="TextFields,HardcodedText" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/editText1"
android:layout_toRightOf="@+id/editText1"
android:text="READ"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Contents of File"
android:inputType="textMultiLine"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="SAVE"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java:
package com.example.ex_no_6;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@SuppressLint("SdCardPath")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e1=(EditText)findViewById(R.id.editText1);
final EditText e2=(EditText)findViewById(R.id.editText2);
Button b1=(Button)findViewById(R.id.button1);
Button b2=(Button)findViewById(R.id.button2);
String path=getPreferences(MODE_PRIVATE).getString("fpath", "/sdcard/file1");
e1.setText(path);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File f=new File(e1.getText().toString());
String s="";
StringBuilder sb=new StringBuilder();
FileReader fr = null;
try {
fr = new FileReader(f);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br=new BufferedReader(fr);
try {
while((s=br.readLine())!=null){
sb.append(s+"\n");
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "File Read
Successfully !!!", Toast.LENGTH_LONG).show();
e2.setText(sb);
}
});
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File f=new File(e1.getText().toString());
FileWriter fw = null;
try {
fw = new FileWriter(f);
}
catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
fw.write(e2.getText().toString());
}
catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
fw.close();
}
catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
e=getPreferences(MODE_PRIVATE).edit();
}
SharedPreferences.Editor
e.putString("fpath", f.getPath());
e.commit();
Toast.makeText(getApplicationContext(), "SavedSuccessfully !!!",
Toast.LENGTH_LONG).show();
}
});
}
}
RESULT: Thus the Above Implement an Application That Writes Data to the Sd Card
Successfully
EX NO: 8 IMPLEMENT AN APPLICATION THAT SEND A SMS AND CREATES AN
ALERT UPON RECEIVING THE SMS
AIM:
To implement an application that creates an alert upon receiving a message in android
PROCEDURE:
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 seprated by comma and click
finish
5. Go to package explorer in the left hand side.select our project.
6. Go to res folder and select layout. Double click the main.xml file. Add the code
7. Now select mainactivity.java file and type the following code.In my coding
maniactivity name is Alert1Activity .
8. Now go to main.xml and right click .select run as option and select run configuration
activity_main.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="com.example.ex_no_10.MainActivity" >
</RelativeLayout>
MainActivity.java
package com.example.ex_no_8;
import android.support.v7.app.ActionBarActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
private static MainActivity inst;
public static MainActivity instance() {
// TODO Auto-generated method stub
return inst;
}
public void onStart() {
super.onStart();
inst = this;
}
NotificationManager nm;
Notification n;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
n = new Notification(R.drawable.ic_launcher, "SMS Alert", System.currentTimeMillis());
}
@SuppressWarnings("deprecation")
public void update_notification(String no, String msg) {
// TODO Auto-generated method stub
n.setLatestEventInfo(getBaseContext(), no, msg, null);
nm.notify(1337, n);
}
}
Receiver.java
package com.example.ex_no_8;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class Receiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String no = null, msg = null;
Bundle b = arg1.getExtras();
if (b != null) {
Object[] sms = (Object[]) b.get(SMS_BUNDLE);
for (int i = 0; i < sms.length; ++i) {
SmsMessage sm = SmsMessage.createFromPdu((byte[]) sms[i]);
no = sm.getOriginatingAddress();
msg = sm.getMessageBody().toString();
}
MainActivity inst = MainActivity.instance();
inst.update_notification(no, msg);
}
}
}
RESULT: Thus implement an application that creates an alert upon receiving a message
executed
PROCEDURE:
Open Android Studio and then click on File -> New -> New project.
activity_main.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="com.example.ex_no_11.MainActivity">
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="71dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="SET ALARM"
tools:ignore="HardcodedText" />
</RelativeLayout>
MainActivity.java
package com.example.ex_no_10;
import java.util.Calendar;
import android.support.v7.app.ActionBarActivity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private static MainActivity inst;
public static MainActivity instance() {
// TODO Auto-generated method stub
return inst;
}
public void onStart() {
super.onStart();
inst = this;
}
NotificationManager nm;
Notification n;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);
final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
Button b = (Button) findViewById(R.id.button1);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
n = new Notification(R.drawable.ic_launcher, "ALARM", System.currentTimeMillis());
tp.setIs24HourView(false);
Calendar now = Calendar.getInstance();
dp.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH), null);
tp.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
tp.setCurrentMinute(now.get(Calendar.MINUTE));
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Calendar current = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(), tp.getCurrentHour(),
tp.getCurrentMinute(), 00);
if (alarm.compareTo(current) <= 0)
Toast.makeText(getApplicationContext(), "Invalid Date and Time !!!",
Toast.LENGTH_LONG).show();
else {
Intent i = new Intent(MainActivity.this, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 123, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), pi);
Toast.makeText(getApplicationContext(), "Alarm is Set ON !!!",
Toast.LENGTH_LONG).show();
}
}
});
}
@SuppressWarnings("deprecation")
public void update_notification(String no, String msg) {
// TODO Auto-generated method stub
n.setLatestEventInfo(getBaseContext(), no, msg, null);
nm.notify(1337, n);
}
}
Alarm.java:
package com.example.ex_no_10;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
MainActivity inst = MainActivity.instance();
inst.update_notification("Alarm", "Wake up ! Wake up !!");
}
}
RESULT:
Thus Android Application that creates Alarm Clock is developed and executed successfully.