0% found this document useful (0 votes)
1K views11 pages

MAD Practical17 TO 22

The document discusses activity lifecycles in Android and provides examples of implementing activity lifecycle methods. Specifically: - It lists the activity lifecycle methods like onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). - It provides code for an example MainActivity class that implements all these methods and uses Log statements to demonstrate when each is called. - It includes the layout XML file for the activity's user interface, which contains a simple "Hello World" text view. - It asks the reader to write programs to open the phone dialer from a button click and load a web page based on text input, providing example code for each.

Uploaded by

Kalyani Reyya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views11 pages

MAD Practical17 TO 22

The document discusses activity lifecycles in Android and provides examples of implementing activity lifecycle methods. Specifically: - It lists the activity lifecycle methods like onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). - It provides code for an example MainActivity class that implements all these methods and uses Log statements to demonstrate when each is called. - It includes the layout XML file for the activity's user interface, which contains a simple "Hello World" text view. - It asks the reader to write programs to open the phone dialer from a button click and load a web page based on text input, providing example code for each.

Uploaded by

Kalyani Reyya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical 17

IX. Practical related questions

1. Draw the activity life cycle diagram

2. Give the hierarchy of directory structure where you store activity file
App->java->package->MainActivity
3. Write the difference between onStop(), onDestroy(), also between onPause and onResume()
methods
onStop() – called when activity is no longer visible to user
onDestroy() – called before activity is destroyed
onPause() – called when activity is not visible to user
onResume() – called when activity will start interacting with the user
X. Exercise

Write a program to create an application to implement the methods of activity class


package com.example.myapplication17_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
protected void onStart() {
super.onStart();
Log.d("life cycle","onStart invoked");
}
protected void onResume() {
super.onResume();
Log.d("Life cycle","onResume invoked");
}
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
protected void onStop() {
super.onStop();
Log.d("life cycle","onStop invoked");
}
protected void onRestart() {
super.onRestart();
Log.d("life cycle","onRestart invoked");
}
protected void onDestroy() {
super.onDestroy();
Log.d("life cycle","onStop invoked");
}
}

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


<androidx.constraintlayout.widget.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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Practical 18

IX. Practical related questions

1. List different methods used in intent.


String getAction()
boolean getBooleanExtra(String name, boolean value)
Set getCategories()
Uri getData()
String getDataString()
Intent putExtra(String n, int v)
void removeExtra(String n)
Intent setAction(String a)
2. Write an intent to display phone dialler with the number

package com.example.myapplication18_3;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener


{
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);

b1.setOnClickListener(this);
}

public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_CALL);


intent.setData(Uri.parse("num"));

if(ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL_PH
ONE) !=PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(intent);
}

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Dial"/>

</LinearLayout>

X. Exercise

1. Write a program to create a text field and a button “Navigate”. When you enter www.google.com
and press navigate button it should open google page
package com.example.myapplication18;

import androidx.appcompat.app.AppCompatActivity;

import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
Button b1;
EditText e1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.myButton);
e1 = (EditText) findViewById(R.id.myText);
b1.setOnClickListener(this);
}

@Override
public void onClick(View v) {
String str = e1.getText().toString();
Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
i.putExtra(SearchManager.QUERY,str);
startActivity(i);
}
}

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


<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter address"
android:id="@+id/myText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myButton"
android:text="Navigate"/>
</LinearLayout>
2.write a program to create button “start dialler”. When you click on this button it should open the
phone dialler

package com.example.myapplication18_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener{
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.myButton);
b1.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
}

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myButton"
android:text="Start Dialer"/>

</LinearLayout>
Practical 21

IX. Practical related questions

1. Differentiate between activity intent and broadcast intent


Android application components can connect to other android apps. Intent can be used to
do this. Intents are asynchronous messages which allow application components to request
from other android components. startActivity() can be used to start an activity.
Broadcast intents are a mechanism by which an intent can be used for consumption by
multiple components on an android system in response to a broadcast message
2. Explain broadcast receiver lifecycle
Broadcast receiver has a single callback method onReceive(). When broadcast message
arrive for the receiver, android system calls its onReceive() method and passes the intent
object containing the message. The receiver is active only when this method executes. When
this method returns, the broadcast receiver is inactive and android system is allowed to
recycle receiver
3. List the system events related to broadcast receivers
Intent.ACTION_BOOT_COMPLETED
Intent.ACTION_POWER_DISCONNECTED
Intent.ACTION_POWER_CONNECTED
Intent.ACTION_BATTERY_LOW
Intent.ACTION_BATTERY_OK

X.Exercise

Write a program to demonstrate system broadcast message


package com.example.myapplication21;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

public MyReceiver receiver;


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

receiver = new MyReceiver();


}
public void onResume() {
super.onResume();
MainActivity.this.registerReceiver(receiver,new
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
}
public void onStop() {
super.onStop();
MainActivity.this.unregisterReceiver(receiver);
}

}
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:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="broadcast message"
android:id="@+id/et1"/>

</LinearLayout>

androidManifest.xml

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


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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

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

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


</intent-filter>

</activity>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE">

</action>
</intent-filter>

</receiver>

</application>

</manifest>

MyReceiver.java
package com.example.myapplication21;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {


public MyReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction()==Intent.ACTION_AIRPLANE_MODE_CHANGED) {
Toast.makeText(context,"Air plane mode
changed",Toast.LENGTH_SHORT).show();
}
}
}
Practical 22

IX. Practical related questions

1. List the best practices for accessing and using sensors


Android devices are manufactured by different OEMs(original equipment manufacturers)
and comes with different configurations
Before using the sensor coordinate system, confirm the default orientation mode of the
device and check for the orientation of X and Y axes
Check the availability range, minimum delay, reporting modes and resolution of sensor
before using it.
2. Differentiate between Sensor class and SensorManager class
Sensor class helps to get information about sensors available. SensorManager class is used
to get the object of the type of sensor.

X.Exercise

1. write a program to change the background colour when device is shuffled


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

>

<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Shake to change background"
android:textColor="#000025"
android:textSize="30sp" />

</RelativeLayout>
package com.example.myapplication22_1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SensorEventListener


{
private SensorManager sensorManager;
private boolean color = false;
private View view;
private long lastUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.t1);
view.setBackgroundColor(Color.GREEN);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);


lastUpdate = System.currentTimeMillis();
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}

}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)/
(SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = event.timestamp;
if (accelationSquareRoot >= 2) //
{
if (actualTime - lastUpdate < 200) {
return;
}
lastUpdate = actualTime;
Toast.makeText(this, "Device was shuffled", Toast.LENGTH_SHORT)
.show();
if (color) {
view.setBackgroundColor(Color.GREEN);
} else {
view.setBackgroundColor(Color.RED);
}
color = !color;
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}

}
2.write a program to display list of sensors supported by mobile device
package com.example.myapplication22_2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


ListView listView ;
SensorManager sensorManager ;
List<Sensor> listsensor;
List<String> liststring ;
ArrayAdapter<String> adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView)findViewById(R.id.listview1);

liststring = new ArrayList<String>();

sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

listsensor = sensorManager.getSensorList(Sensor.TYPE_ALL);

for(int i=0; i<listsensor.size(); i++){

liststring.add(listsensor.get(i).getName());
}

adapter = new ArrayAdapter<String>(MainActivity.this,


android.R.layout.simple_list_item_2,
android.R.id.text1, liststring
);

listView.setAdapter(adapter);

}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECEFF1">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/listview1"/>

</RelativeLayout>

You might also like