Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Sensor in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 156
    Comment on it

    If you want to create Sensor function you can use my below example code. The android platform supports three categories of sensors. 1st Motion Sensors, 2nd Environmental sensors and 3rd is Position sensors. In the below code I have used SensorEventListener method. See the below example it will clearly desdcribed you how to make sensor function.

    Step(1)-activity_main.xml

    <RelativeLayout xmlns:androclass="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" >  
    
        <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="85dp"  
            android:layout_marginTop="104dp"  
            android:text="Sensor Device" />  
    
    </RelativeLayout>  
    

    Step(2)-MainActivity-

    public class MainActivity extends Activity {  
        SensorManager sm = null;  
        TextView textView1 = null;  
        List list;  
    
        SensorEventListener sel = new SensorEventListener(){  
            public void onAccuracyChanged(Sensor sensor, int accuracy) {}  
            public void onSensorChanged(SensorEvent event) {  
                float[] values = event.values;  
                textView1.setText("x: "+values[0]+"\ny: "+values[1]+"\nz: "+values[2]);  
            }  
        };  
    
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
    
            /* Get a SensorManager instance */  
            sm = (SensorManager)getSystemService(SENSOR_SERVICE);  
    
            textView1 = (TextView)findViewById(R.id.textView1);  
    
            list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);  
            if(list.size()>0){  
                sm.registerListener(sel, (Sensor) list.get(0), SensorManager.SENSOR_DELAY_NORMAL);  
            }else{  
                Toast.makeText(getBaseContext(), "Error: No Accelerometer.", Toast.LENGTH_LONG).show();  
            }  
        }  
    
        @Override  
        protected void onStop() {  
            if(list.size()>0){  
              sm.unregisterListener(sel);  
            }  
            super.onStop();  
        }  
    }  
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: