Saturday 16 November 2013

Android Sensor Example

                                    Android Sensor Example

Welcome Friend,
            In this example i going to show a list of Sensor on a ListView that are available in device.In last Sensor post i told you how we create sensor base example.

And also explain Proximity sensor :- Proximity sensor sense the object minimum 0 to approx 2.5 cm.
so if any object come nearer to 2.5 cm from sensor it can generate some event.

Code for layout xml file: 

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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" >

   
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Scan Sensor" />

   
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

   
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="216dp" >

   

   
        android:id="@+id/imageSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom" >

   


package in.androidshivendra.androidsensorexample;

import java.util.List;



import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener ,ViewFactory{

Button bscan;
ListView lv;
SensorManager smgr;
List msensor;

ImageSwitcher iv;
Sensor ses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.activity_main);
bscan = (Button)findViewById(R.id.button1);
lv = (ListView)findViewById(R.id.listView1);
smgr = (SensorManager)getSystemService(SENSOR_SERVICE);
msensor=smgr.getSensorList(Sensor.TYPE_ALL);
iv = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
iv.setFactory(this);
final ArrayAdapter asens = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,msensor);
bscan.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lv.setAdapter(asens);
}});
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, msensor.get(pos).getName(), Toast.LENGTH_SHORT).show();
}
});
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
Log.d("Sensor example", e.toString());
}
}

@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 sensor, int accuracy) {
// TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.values[0] == 0){
iv.setImageResource(R.drawable.near);
}else{
iv.setImageResource(R.drawable.far);
}
}
protected void onResume() {
super.onResume();
ses = smgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);
smgr.registerListener(this, ses, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {
super.onPause();
smgr.unregisterListener(this);
}

@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return iv;
}

}


Note: This application only Test on Real Device.You can download Application here


No comments:

Today's Pageviews