Android TutorialsImplementing Sensor In Android

Implementing Sensor In Android

Hello firends!!! In this tutorial we are going to implement Motion Sensor .

  • All we will do in this tutorial is on shaking the device we will navigate from one page to another.
  • So follow the Steps :
    1. Create a new project and for example name it as AppCheck.
    2. Your MainActivity and activity_main files will be generated respectively.
    3. Now go to your activity_main.xml file and in your graphical Layout take LargeText , and in the text property write “Shake to get a toast and to switch Image”,and add the following statement in RelativeLayout :
    4. [code language=”xml”]android:background=”@drawable/your image name”
      [/code]

    5. Hence for this add images in your res-> drawable-hdpi folder
    6. So now your activity_main.xml file graphical layout will look as shown below :
    7. 1

    8. The code of activity_main.xml file is given below :
    9. [code language=”xml”]<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"
      tools:context=".MainActivity"
      android:background="@drawable/q6(your image name)">

      <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="18dp"
      android:text="Shake to Open Next Activity"
      android:textColor="#800000"
      android:textSize="20sp" />

      </RelativeLayout>

      [/code]

    10. Now go to your MainActivity.Java file write the following code as explained:
    11. [code language=”java”]public class MainActivity extends Activity implements SensorEventListener {

      SensorManager sm;
      long lastTime;

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

      sm = (SensorManager) getSystemService(SENSOR_SERVICE);
      lastTime = System.currentTimeMillis();
      }

      @Override
      public void onAccuracyChanged(Sensor arg0, int arg1)
      {
      // TODO Auto-generated method stub

      }

      @Override
      public void onSensorChanged(SensorEvent event)
      {
      // TODO Auto-generated method stub

      if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
      {
      getAccelerometer(event);
      }
      }

      private void getAccelerometer(SensorEvent event)
      {

      float[] value = event.values;

      float x = value[0];
      float y = value[1];
      float z = value[2];

      float accelationSquareRoot = (x*x + y*y + z*z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);

      long actualTime = System.currentTimeMillis();

      if(accelationSquareRoot >= 2)
      {

      if(actualTime-lastTime < 200)
      {
      return;
      }

      lastTime = actualTime;

      // Perform your Action Here..

      Intent i = new Intent(MainActivity.this, NextPage.class);
      startActivity(i);

      Toast.makeText(this, "Your Next Activity is successfully called",Toast.LENGTH_SHORT).show();
      finish();
      }
      }

      @Override
      protected void onPause()
      {
      // TODO Auto-generated method stub
      super.onPause();
      sm.unregisterListener(this);
      }

      @Override
      protected void onResume()
      {
      // TODO Auto-generated method stub
      super.onResume();
      sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
      }
      }

      [/code]

    12. So now your window will look as shown below :
    13. 3
      4
      5

    14. Now create another class and name it NextPage2 and also create an android xml file for this class and name it as next2.
    15. Write the following code in NextPage2.Java :
    16. [code language=”java”]public class NextPage2 extends Activity{

      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.next2);
      }

      @Override
      public void onBackPressed() {
      // TODO Auto-generated method stub

      finish();
      startActivity(new Intent(NextPage2.this,MainActivity .class));
      }

      }

      [/code]

    17. Hence the window will look as shown below :
    18. 6

    19. Now go to your next2.xml file and only set the background image in the layout by adding the following statement:

      android:background=”@drawable/your image name”
    20. Hence the code is:
    21. [code language=”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"
      android:background="@drawable/q7(your image name)">

      </LinearLayout>

      [/code]

    22. Your window will look as shown below :
    23. 7

    24. Now go to your AndroidManifest.xml file and add an activity into it to make the intent successful :
    25. 8

    26. Now run your app and you will have the following output :
    27. 1

    28. Now shake your device and you will be navigated to the next page:
    29. 9

    30. Thus we have successfully implemented sensors.

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -