Implementing Sensor In Android

0
1526

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. android:background=”@drawable/your image name”
      

    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. The code of activity_main.xml file is given below :
    8. <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>
      
      

    9. Now go to your MainActivity.Java file write the following code as explained:
    10. 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);
      	}
      }
      
      

    11. So now your window will look as shown below :

    12. 4

      5

    13. Now create another class and name it NextPage2 and also create an android xml file for this class and name it as next2.
    14. Write the following code in NextPage2.Java :
    15. 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));		
      	}
      	
      }
      
      

    16. Hence the window will look as shown below :

    17. 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”
    18. Hence the code is:
    19. <?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>
      
      

    20. Your window will look as shown below :

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

    22. Now run your app and you will have the following output :

    23. Now shake your device and you will be navigated to the next page:

    24. Thus we have successfully implemented sensors.