Mobile Application Program-Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

5.

Develop an application that makes use of Notification Manager

6. Implement an application that uses Multi-threading

1.activity_main.xml

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


<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<TableLayout
android:layout_width="395dp"
android:layout_height="715dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multithreading Application"
tools:layout_editor_absoluteX="176dp"
tools:layout_editor_absoluteY="16dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value from thread1:"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="41dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="@+id/no1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="0"
tools:layout_editor_absoluteX="98dp"
tools:layout_editor_absoluteY="49dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value from thread2"
tools:layout_editor_absoluteX="156dp"
tools:layout_editor_absoluteY="108dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="@+id/no2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="0"
tools:layout_editor_absoluteX="78dp"
tools:layout_editor_absoluteY="124dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="182dp"
android:onClick="start"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
</android.support.constraint.ConstraintLayout>

2.MainActivity.java
package com.example.bii_exno_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Handler tss_handler=new Handler();
EditText tss_tv1,tss_tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tss_tv1=(EditText)findViewById(R.id.no1);
tss_tv2=(EditText)findViewById(R.id.no2);
}
public void thread1()
{
tss_tv1.setText(String.valueOf(Integer.parseInt(tss_tv1.getText().toString())+1));
tss_handler.postDelayed(run,500);
}
public void thread2()
{
tss_tv2.setText(String.valueOf(Integer.parseInt(tss_tv2.getText().toString())+1));
tss_handler.postDelayed(run,500);
}
public void start(View view)
{
tss_handler.postDelayed(run,500);
}
Runnable run=new Runnable() {
@Override
public void run() {
thread1();
thread2();
}
};
}

7. Develop a native application that uses GPS location information

1.activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/lat"
android:textSize="20sp"
android:text="Latitude :"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lon"
android:textSize="20sp"
android:text="Longitude :"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

2.MainActivity.java

package com.example.student.maps;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
TextView latitude;
TextView longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude = (TextView) findViewById(R.id.lat);
longitude = (TextView) findViewById(R.id.lon);
locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude.setText("Latitude :"+location.getLatitude());
longitude.setText("Longitude :"+location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
}
}

8. Implement an application that writes data to the SD card.

1.activity_main.xml

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


<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<TableLayout
android:layout_width="395dp"
android:layout_height="715dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="162dp"
tools:layout_editor_absoluteY="32dp" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="98dp"
tools:layout_editor_absoluteY="49dp" />

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write"
tools:layout_editor_absoluteX="130dp"
tools:layout_editor_absoluteY="99dp"
android:onClick="write"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="146dp"
tools:layout_editor_absoluteY="147dp"
android:onClick="read"/>
</TableLayout>
</android.support.constraint.ConstraintLayout>

2.MainActivity.java

package com.example.bii_exno_8;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText text=(EditText) findViewById(R.id.write);
}
public void write(View view)
{
try
{
FileOutputStream fout=openFileOutput("CSE-B.txt",MODE_PRIVATE);
OutputStreamWriter writer1=new OutputStreamWriter(fout);
EditText text=(EditText) findViewById(R.id.write);
writer1.write(text.getText().toString());
writer1.close();
Toast.makeText(getBaseContext(),"File saved
sucessfully",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{

}
}
public void read(View view)
{
try {
FileInputStream filein = openFileInput("CSE-B.txt");
InputStreamReader reader=new InputStreamReader(filein);
char[] inputbuffer=new char[1000];
String txt="";
int charread;
while((charread=reader.read(inputbuffer))>0)
{
String read=String.copyValueOf(inputbuffer,0,charread);
txt+=read;
}
reader.close();
Toast.makeText(getBaseContext(),txt,Toast.LENGTH_LONG).show();
}
catch (Exception e)
{

}
}
}

You might also like