WordPress.com



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.

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

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

[pic]

• So now the designing part is completed.

Java Coding for the Android Application:

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

[pic]

• 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:

[pic]        [pic]        [pic]

[pic]        [pic]        [pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Creating Second Activity for the Android Application:

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

[pic]

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

[pic]

• 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.

[pic]

• Now click on Text as shown below.

[pic]

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

     

        

    

    

        

 

 

 

 

 

  

 

Code for Activity_main.xml:

• Now click on Design and your activity will look as given below.

[pic]

• So now the designing part of Main Activity is completed.

Designing Layout for Second Activity:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_second.xml:

| | |

| | |

| |  |

| |     |

| |  |

| |     |

| |  |

| |     |

| |  |

| | |

• Now click on Design and your activity will look as given below.

[pic]

• 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.

[pic]

• 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.

[pic]

• 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:

[pic]        [pic]        [pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

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

[pic]

• So now the designing part is completed.

Java Coding for the Android Application:

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

[pic]

• 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:

[pic]        [pic]        [pic]

 

[pic]        [pic]        [pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

?

|1 | |

|2 | |

|5 |  |

|6 |     |

|10| |

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

[pic]

• So now the designing part is completed.

Java Coding for the Android Application:

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

[pic]

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

Code for MainActivity.java:

?

|1 |package com.example.exno4; |

|2 |  |

|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; |

|10|import android.widget.ImageView; |

|11|  |

|12|public 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:

[pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

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

[pic]

• So now the designing part is completed.

Java Coding for the Android Application:

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

[pic]

• 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:

[pic]        [pic]        [pic]

[pic]        [pic]        [pic]

[pic]        [pic]        [pic]

[pic]        [pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

?

|1 | |

|2 | |

|6 |  |

|7 |     |

|11|  |

|12| |

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

[pic]

• So now the designing part is completed.

Adding permissions in Manifest for the Android Application:

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

[pic]

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

[pic]

Code for AndroidManifest.xml:

?

|1 | |

|2 | |

|4 |  |

|5 |     |

|6 |  |

|7 |     |

|13|         |

|14|             |

|15|                 |

|16|  |

|17|                 |

|18|             |

|19|         |

|20|     |

|21|  |

|22| |

• So now the Permissions are added in the Manifest.

Java Coding for the Android Application:

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

[pic]

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

Code for MainActivity.java:

?

|1 |package com.example.exno6; |

|2 |  |

|3 |import android.app.ListActivity; |

|4 |import android.content.Intent; |

|5 |import .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 .MalformedURLException; |

|17 |import .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 |

|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(""); |

|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, etc.. |

|52 |                int eventType = xpp.getEventType(); |

|53 |                while (eventType != XmlPullParser.END_DOCUMENT) |

|54 |                { |

|55 |                    if (eventType == XmlPullParser.START_TAG) |

|56 |                    { |

|57 |                        if (xpp.getName().equalsIgnoreCase("item")) |

|58 |                        { |

|59 |                            insideItem = true; |

|60 |                        } |

|61 |                        else if (xpp.getName().equalsIgnoreCase("title")) |

|62 |                        { |

|63 |                            if (insideItem) |

|64 |                                headlines.add(xpp.nextText()); //extract the headline |

|65 |                        } |

|66 |                        else if (xpp.getName().equalsIgnoreCase("link")) |

|67 |                        { |

|68 |                            if (insideItem) |

|69 |                                links.add(xpp.nextText()); //extract the link of article |

|70 |                        } |

|71 |                    } |

|72 |                    else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) |

|73 |                    { |

|74 |                        insideItem=false; |

|75 |                    } |

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

|77 |                } |

|78 |  |

|79 |            } |

|80 |            catch (MalformedURLException e) |

|81 |            { |

|82 |                e.printStackTrace(); |

|83 |            } |

|84 |            catch (XmlPullParserException e) |

|85 |            { |

|86 |                e.printStackTrace(); |

|87 |            } |

|88 |            catch (IOException e) |

|89 |            { |

|90 |                e.printStackTrace(); |

|91 |            } |

|92 |            return null; |

|93 |        } |

|94 |        protected void onPostExecute(ArrayAdapter adapter) |

|95 |        { |

|96 |            adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, headlines); |

|97 |            setListAdapter(adapter); |

|98 |        } |

|99 |    } |

|100 |  |

|101 |    @Override |

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

|103 |    { |

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

|105 |        Intent intent = new Intent(Intent.ACTION_VIEW, uri); |

|106 |        startActivity(intent); |

|107 |    } |

|108 |  |

|109 |    public InputStream getInputStream(URL url) |

|110 |    { |

|111 |        try |

|112 |        { |

|113 |            return url.openConnection().getInputStream(); |

|114 |        } |

|115 |        catch (IOException e) |

|116 |        { |

|117 |            return null; |

|118 |        } |

|119 |    } |

|120 |} |

• So now the Coding part is also completed.

• Now run the application to see the output.

Output:

[pic]  [pic]  [pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

?

|1 | |

|2 | |

|7 |  |

|8 |     |

|14|  |

|15|     |

|22|  |

|23|     |

|30|  |

|31|     |

|38|  |

|39| |

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

[pic]

• So now the designing part is completed.

Adding permissions in Manifest for the Android Application:

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

[pic]

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

[pic]

Code for AndroidManifest.xml:

?

|1 | |

|2 | |

|4 |  |

|5 |     |

|6 |  |

|7 |     |

|13|         |

|14|             |

|15|                 |

|16|  |

|17|                 |

|18|             |

|19|         |

|20|     |

|21| |

• So now the Permissions are added in the Manifest.

Java Coding for the Android Application:

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

[pic]

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

Code for MainActivity.java:

?

|1 |package com.example.exno9; |

|2 |  |

|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; |

|9 |  |

|10|import java.io.BufferedReader; |

|11|import java.io.File; |

|12|import java.io.FileInputStream; |

|13|import java.io.FileOutputStream; |

|14|import java.io.InputStreamReader; |

|15|  |

|16|public class MainActivity extends AppCompatActivity |

|17|{ |

|18|    EditText e1; |

|19|    Button write,read,clear; |

|20|    @Override |

|21|    protected void onCreate(Bundle savedInstanceState) |

|22|    { |

|23|        super.onCreate(savedInstanceState); |

|24|        setContentView(R.layout.activity_main); |

|25|  |

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

|27|        write= (Button) findViewById(R.id.button); |

|28|        read= (Button) findViewById(R.id.button2); |

|29|        clear= (Button) findViewById(R.id.button3); |

|30|  |

|31|        write.setOnClickListener(new View.OnClickListener() |

|32|        { |

|33|            @Override |

|34|            public void onClick(View v) |

|35|            { |

|36|                String message=e1.getText().toString(); |

|37|                try |

|38|                { |

|39|                    File f=new File("/sdcard/myfile.txt"); |

|40|                    f.createNewFile(); |

|41|                    FileOutputStream fout=new FileOutputStream(f); |

|42|                    fout.write(message.getBytes()); |

|43|                    fout.close(); |

|44|                    Toast.makeText(getBaseContext(),"Data Written in SDCARD",Toast.LENGTH_LONG).show(); |

|45|                } |

|46|                catch (Exception e) |

|47|                { |

|48|                    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show(); |

|49|                } |

|50|            } |

|51|        }); |

|52|  |

|53|        read.setOnClickListener(new View.OnClickListener() |

|54|        { |

|55|            @Override |

|56|            public void onClick(View v) |

|57|            { |

|58|                String message; |

|59|                String buf = ""; |

|60|                try |

|61|                { |

|62|                    File f = new File("/sdcard/myfile.txt"); |

|63|                    FileInputStream fin = new FileInputStream(f); |

|64|                    BufferedReader br = new BufferedReader(new InputStreamReader(fin)); |

|65|                    while ((message = br.readLine()) != null) |

|66|                    { |

|67|                        buf += message; |

|68|                    } |

|69|                    e1.setText(buf); |

|70|                    br.close(); |

|71|                    fin.close(); |

|72|                    Toast.makeText(getBaseContext(),"Data Recived from SDCARD",Toast.LENGTH_LONG).show(); |

|73|                } |

|74|                catch (Exception e) |

|75|                { |

|76|                    Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show(); |

|77|                } |

|78|            } |

|79|        }); |

|80|  |

|81|        clear.setOnClickListener(new View.OnClickListener() |

|82|        { |

|83|            @Override |

|84|            public void onClick(View v) |

|85|            { |

|86|                e1.setText(""); |

|87|            } |

|88|        }); |

|89|    } |

|90|} |

• So now the Coding part is also completed.

• Now run the application to see the output.

Output:

[pic] [pic] [pic]

[pic][pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Creating Second Activity for the Android Application:

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

[pic]

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

[pic]

• Thus Second Activity For the application is created.

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

?

|1 | |

|2 | |

|7 |  |

|8 |     |

|13|  |

|14|     |

|20|  |

|21|     |

|29|  |

|30| |

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

[pic]

• So now the designing part is completed.

Java Coding for the Android Application:

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

[pic]

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

Code for MainActivity.java:

?

|1|package com.example.exno10; |

|2|  |

|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|public class MainActivity extends AppCompatActivity |

|1|{ |

|2|    Button notify; |

|1|    EditText e; |

|3|    @Override |

|1|    protected void onCreate(Bundle savedInstanceState) |

|4|    { |

|1|        super.onCreate(savedInstanceState); |

|5|        setContentView(R.layout.activity_main); |

|1|  |

|6|        notify= (Button) findViewById(R.id.button); |

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

|7|  |

|1|        notify.setOnClickListener(new View.OnClickListener() |

|8|        { |

|1|            @Override |

|9|            public void onClick(View v) |

|2|            { |

|0|                Intent intent = new Intent(MainActivity.this, SecondActivity.class); |

|2|                PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); |

|1|                Notification noti = new Notification.Builder(MainActivity.this).setContentTitle("New |

|2|Message").setContentText(e.getText().toString()).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pending).build(); |

|2|                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |

|2|                noti.flags |= Notification.FLAG_AUTO_CANCEL; |

|3|                manager.notify(0, noti); |

|2|            } |

|4|        }); |

|2|    } |

|5|} |

|2| |

|6| |

|2| |

|7| |

|2| |

|8| |

|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:

[pic]  [pic]

[pic]  [pic]

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.

[pic]

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

[pic]

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

[pic]

• Then select the Empty Activity and click Next. 

[pic]

• Finally click Finish.

[pic]

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

• After completion it will look as given below.

[pic]

Creating Second Activity for the Android Application:

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

[pic]

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

[pic]

• Thus Second Activity For the application is created.

Designing layout for the Android Application:

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

[pic]

• Now click on Text as shown below.

[pic]

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

Code for Activity_main.xml:

?

|1 | |

|2 | |

|6 |  |

|7 |     |

|12|  |

|13|     |

|21|  |

|22| |

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

[pic]

• So now the designing part is completed.

Changes in Manifest for the Android Application:

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

[pic]

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

[pic]

Code for AndroidManifest.xml:

?

|1 | |

|2 | |

|4 |  |

|5 |     |

|11|         |

|12|             |

|13|                 |

|14|  |

|15|                 |

|16|             |

|17|         |

|18|         |

|19|         |

|20|     |

|21|  |

|22| |

• 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.

[pic]

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

Code for MainActivity.java:

?

|1 |package com.example.exno11; |

|2 |  |

|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; |

|10|import android.widget.Toast; |

|11|import android.widget.ToggleButton; |

|12|  |

|13|import java.util.Calendar; |

|14|  |

|15|public 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, alarmTimePicker.getCurrentHour()); |

|37|            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute()); |

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

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

|40|  |

|41|            time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000)); |

|42|            if(System.currentTimeMillis()>time) |

|43|            { |

|44|                if (calendar.AM_PM == 0) |

|45|                    time = time + (1000*60*60*12); |

|46|                else |

|47|                    time = time + (1000*60*60*24); |

|48|            } |

|49|            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent); |

|50|        } |

|51|        else |

|52|        { |

|53|            alarmManager.cancel(pendingIntent); |

|54|            Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show(); |

|55|        } |

|56|    } |

|57|} |

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

Java Coding for Alarm Receiver:

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

[pic]

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

Code for AlarmReceiver.java:

?

|1 |package com.example.exno11; |

|2 |  |

|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 .Uri; |

|9 |import android.widget.Toast; |

|10|  |

|11|public 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 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); |

|18|        if (alarmUri == null) |

|19|        { |

|20|            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |

|21|        } |

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

|23|        ringtone.play(); |

|24|    } |

|25|} |

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

• Now run the application to see the output.

Output:

[pic]      [pic]

[pic]      [pic]

Result:

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

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download