Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Example of Sensor in Android Application

 

In today's video we are going to see an example of Sensor in Android.

For this we will post take one relative layout in our activity_main.xml file and also set basic properties to the Text view.

Now we will open MainActivity.java file and code inside it.

public class MainActivity extends Activity

{

SensorManager sm=null;

TextView accText=null;

List list;

SensorEventListener sel=new SensorEventListener ()

{

@Override

public void onSensorChanged(SensorEvent event)

{

float[] values=event.values;

accText.setText("x:"+values[0]+"\\ny:"+values[1]+"\\nz:"+values[2]);

}

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy)

}

}

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sm(SensorManager)getSystemService(SEARCH_SERVICE);

After that we will pass 3 different content, sm, accText, and list.

The condition which we need to set now is,

if(list.size()>0)

{

sm.registerListener(sel,(Sensor)list.get(0),SensorManager.SENSOR_DELAY_NORMAL);

}

else {

Toast.makeText(getBaseContext(),"Error",Toast.LENGTH_LONG);

}

After coding all of this we will run our code. We see that our code runs successfully. By default the values of X,Y,Z is printed and as we rotate our phone the values get changed accordingly.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere