Practical 22
Practical 22
Exercise :-
1). Write a program to changes the background color when device is shuffled.
Code :-
ACITIVITY_MAIN.XML
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);
}
}
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 :-