Practical No 22
Practical No 22
Practical No 22
22
The next thing you need to do is to instantiate the object of Sensor class by calling the
getDefaultSensor() method of the SensorManager class. Its syntax is given below: Sensor light;
light = sMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
Once that sensor is declared, you need to register its listener and override two methods
which are onAccuracyChanged and onSensorChanged. Its syntax is as follows:
Methods:
1. getDefaultSensor(int type) :- This method get the default sensor for a given type. Explain
methods
2. getOrientation(float[] R, float[] values) :- This method returns a description of the current
primary clip on the clipboard but not a copy of its data.
3. getInclination(float[] I) :- This method computes the geomagnetic inclination angle in
radians from the inclination matrix.
4. registerListener(SensorListener listener, int sensors, int rate) :-This method registers a
listener for the sensor
5. unregisterListener(SensorEventListener listener, Sensor sensor) :-This method unregisters
a listener for the sensors with which it is registered.
6. getOrientation(float[] R, float[] values) :-This method computes the device's orientation
based on the rotation matrix.
7. getAltitude(float p0, float p) :-This method computes the Altitude in meters from the
atmospheric pressure and the pressure at sea-level.
2.
SensorManager: This is used to get access to various sensors present in the device to use it
according to need.
Sensor: This class is used to create an instance of a specific sensor.
X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to changes the background color when device is shuffled.
2. Write a program to display the list of sensors supported by the mobile device.
package com.jamiapolytechnic.experiment221;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
import android.hardware.Sensor;
import java.util.List;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Get a SensorManager instance */
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
txtView1 = (TextView)findViewById(R.id.txtView1);
txtView1.setTextSize(20);
list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(list.size()>0){
sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);
}else{
Toast.makeText(getBaseContext(), "Error: No Accelerometer.",
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStop() {
if(list.size()>0){
sm.unregisterListener(sel);
}
super.onStop();
}
//=====================================================================
//activity_main.xml
<TextView
android:id="@+id/txtView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
//=====================================================================
2.
MainActivity.java
package com.jamiapolytechnic.experiment222;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//=====================================================================
//activity_main.xml
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Sensors"
android:textSize="20dp"
android:textStyle="bold"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>
Dated signature of
Marks Obtained
Teacher
Process Product Total
Related(10) Related(15) (25)