Thursday, 3 April 2014

motion, proximity sensor function in android

Motion sensors are useful for monitoring device movement, such as tilt, shake, rotation ,swing. Motion sensors have x,y,z axis which changes as per the movement of smartphone.

MainActivity.java
 public class MainActivity extends Activity implements SensorEventListener {  
      int sx,sy;  
      TextView t1;  
      TextView t2;  
      TextView t3;  
      TextView t4;  
      int count;  
      int a;  
      private SensorManager sensorMan;  
      private Sensor accelerometer;  
      Sensor proximity ;  
      private float[] mGravity;  
      @SuppressWarnings({ "deprecation"})  
      @SuppressLint("NewApi")  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           //getting the screen size   
     Display display = getWindowManager().getDefaultDisplay();  
                sx = display.getWidth();  
                sy = display.getHeight();;  
                t1=(TextView) findViewById(R.id.textView1);  
                t2=(TextView) findViewById(R.id.textView2);  
                t3=(TextView) findViewById(R.id.textView6);  
                sensorMan = (SensorManager)getSystemService(SENSOR_SERVICE);  
                accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
                proximity = sensorMan.getDefaultSensor(Sensor.TYPE_PROXIMITY);  
                t2.setText("x= "+sx+" y= "+sy);  
      }  
       @Override  
       public void onResume()   
       {  
         super.onResume();  
         sensorMan.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_UI);  
         sensorMan.registerListener(this, proximity,SensorManager.SENSOR_DELAY_UI);  
       }  
       @Override  
       protected void onPause()   
       {  
         super.onPause();  
         sensorMan.unregisterListener(this);  
       }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      @Override  
      public void onAccuracyChanged(Sensor arg0, int arg1) {  
           // TODO Auto-generated method stub  
      }  
       @Override  
       public void onSensorChanged(SensorEvent event)   
       {  
         mGravity = event.values;  
           // Shake detection  
           float x = mGravity[0];  
           float y = mGravity[1];  
           float z = mGravity[2];  
           t1.setText("\nx="+x+"\ny="+y+"\nz="+z);  
           if(event.sensor.getType() == Sensor.TYPE_PROXIMITY)  
              {  
                t3.setText(""+event.values[0]);  
                // t3.setText(String.valueOf(event.values[0]));  
              }  
       }  
 }  

activity_main.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"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView2"  
     android:layout_alignParentTop="true"  
     android:layout_marginTop="59dp"  
     android:text="@string/t1"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView3"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentTop="true"  
     android:text="@string/large_text"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView2"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginBottom="138dp"  
     android:text="@string/t2"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView4"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView3"  
     android:layout_centerVertical="true"  
     android:text="@string/ss"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView5"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_alignParentLeft="true"  
     android:layout_marginBottom="32dp"  
     android:text="@string/proximity"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <TextView  
     android:id="@+id/textView6"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignBaseline="@+id/textView5"  
     android:layout_alignBottom="@+id/textView5"  
     android:layout_toRightOf="@+id/textView4"  
     android:text="@string/t3"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </RelativeLayout>