flipkart

Tuesday, November 18, 2014

Accelerometer using Android mobile phone

Hi friends ,
  Today I am going to explain Accelerometer using existing hardware inside  Android mobile first you have to know why we have to learn Accelerometer answer is i think all most android apps they using indirectly some sensors preexisting mobile hardware and especially Accelerometer is used swipe application and touch based application so Accelerometer is very use-full sensor one of existing.
Today this i will discuss with Accelerometer  sensor following is my source code of project.

activity_accelerometer.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="com.example.accelerometer.AccelerometerActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="52dp"
        android:textSize="24dp"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
         android:textSize="24dp"
         />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
         android:textSize="24dp"
         />

</RelativeLayout>


This layout of project it have 3 Text-view one for X value ,2nd for Y value and 3rd for Z value

AccelerometerActivity.java

package com.example.accelerometer;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class AccelerometerActivity extends ActionBarActivity implements SensorEventListener {
TextView tvx,tvy,tvz;
SensorManager sensormanager;

@SuppressLint("ServiceCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);
tvx=(TextView) findViewById(R.id.textView1);
tvy=(TextView) findViewById(R.id.textView2);
tvz=(TextView) findViewById(R.id.textView3);
sensormanager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensormanager.registerListener(this, sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.accelerometer, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

// assign directions
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];

tvx.setText("X: "+x);
tvy.setText("Y: "+y);
tvz.setText("Z: "+z);
}

}


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

}
}

This java file inside first sensor initialization with type of Accelerometer sensor and implements SensorEventListener when values are changed it will onSensorChanged subroutine (function) you can see following video is my out of above code 


This is video of my Accelerometer project any it comes to end you any doubts and comments will be accepted 

No comments:

Post a Comment