0% found this document useful (0 votes)
19 views5 pages

Practical 22

The document contains two exercises related to mobile application development. The first exercise involves writing a program that changes the background color of a view when the device is shuffled, utilizing the accelerometer sensor. The second exercise displays a list of all sensors supported by the mobile device, showing their names in a TextView.

Uploaded by

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

Practical 22

The document contains two exercises related to mobile application development. The first exercise involves writing a program that changes the background color of a view when the device is shuffled, utilizing the accelerometer sensor. The second exercise displays a list of all sensors supported by the mobile device, showing their names in a TextView.

Uploaded by

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

Subject :- Mobile Application Development Subject Code :- 22617

Name :- Shivam Sainath Korpakwad Batch :- CO 6 IA Roll No. 24

Exercise :-

1). Write a program to changes the background color when device is shuffled.

Code :-

ACITIVITY_MAIN.XML

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


<RelativeLayout xmlns:android="http://schemas.android
.com/apk/res/android" xmlns:tools="http://schemas.android.co m/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="color" />
</RelativeLayout>

MAINACTIVITY.JAVA

package com.example.practical22;
import android.app.Activity;
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.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener
{
private SensorManager sensorManager;
private boolean isColor = false; private View view;
private long lastUpdate;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.textView);
view.setBackgroundColor(Color.GREE N);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
@Override public void
onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event)
{
float[] values = event.values;
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 = System.currentTimeMillis();
Toast.makeText(getApplicationContext(), String.valueOf(accelationSquareRoot)+ " " +
SensorManager.GRAVITY_EARTH, Toast.LENGTH_SHORT).show();
if (accelationSquareRoot >= 2)
{
if (actualTime - lastUpdate < 200)
{
return;
}

lastUpdate = actualTime;
lastUpdate for next shuffle if (isColor)
{
view.setBackgroundColor(Color.GREE N);
} else { view.setBackgroundColor(Color.RED);
}
isColor = !isColor;
}
}
@Override
protected void onResume()
{
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NO RMAL);
}
}

lastUpdate = actualTime; lastUpdate for next shuffle if (isColor)


{
view.setBackgroundColor(Color.GREE N);
}
else { view.setBackgroundColor(Color.RED);
}
isColor = !isColor;
}
}
@Override
protected void onResume()
{
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NO RMAL);
}

Output :-
2). Write a program to display the list of sensors supported by the mobile device.

Code :-
ACTIVIY_MAIN.XML
<?xml version="1.0" encoding="utf- 8"?>
<RelativeLayout xmlns:android="http://schemas.android
.com/apk/res/android" xmlns:tools="http://schemas.android.co m/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" android:padding="10dp">
<TextView android:id="@+id/tv"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello
World!" />
</RelativeLayout>
MAINACTIVITY.JAVA
package com.example.practical222;
import android.hardware.Sensor; import android.hardware.SensorManager; import
android.os.Bundle;
import android.widget.TextView; import androidx.appcompat.app.AppCompatA ctivity;
import java.util.List;
public class MainActivity extends
AppCompatActivity { TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv); String sensorInfo = "";
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE
);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.T YPE_ALL);
for(Sensor s : sensorList) { sensorInfo += s.getName() + "\n";
}
tv.setText(sensorInfo);
}
}
Output :-

You might also like