Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package com.example.controller;



import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
//import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements SensorEventListener {
	
	private float lastX,lastY,lastZ;
	private SensorManager senMan;
	private Sensor accMeter;
	
	private float deltaX = 0;
	private float deltaY = 0;
	private float deltaZ = 0;
	
	private float deltaXMax = 0;
	private float deltaYMax = 0;
	private float deltaZMax = 0;
		
	private TextView currentX, currentY, currentZ, maxX, maxY, maxZ;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeViews();
        senMan=(SensorManager)this.getSystemService(SENSOR_SERVICE);
        if(senMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)!=null)
        {
        	accMeter=senMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        	senMan.registerListener((SensorEventListener) this, accMeter, SensorManager.SENSOR_DELAY_NORMAL);
        	  	
        }
        
        
    }
	
	
	
	protected void onResume(){
		super.onResume();
		senMan.registerListener((SensorEventListener) this, accMeter,SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	@SuppressWarnings("deprecation")
	protected void onPuse()
	{
		super.onPause();
		senMan.unregisterListener(this);
	}
	
	public void initializeViews() {
		currentX = (TextView) findViewById(R.id.currentX);
		currentY = (TextView) findViewById(R.id.currentY);
		currentZ = (TextView) findViewById(R.id.currentZ);
		maxX=(TextView) findViewById(R.id.maxX);
		maxY=(TextView) findViewById(R.id.maxY);
		/*maxX = (TextView) findViewById(R.id.maxX);
		maxY = (TextView) findViewById(R.id.maxY);
		maxZ = (TextView) findViewById(R.id.maxZ);*/
	}
	
	public void onSensorChanged(SensorEvent event){
		//clear();
		displayValue();
		
		deltaX = Math.abs(lastX - event.values[0]);
		deltaY = Math.abs(lastY - event.values[1]);
		deltaZ= Math.abs(lastZ- event.values[2]);
		
		if (deltaX > 0){
			
			maxX.setText("Left");
			//deltaX=0;
		}
		else if(deltaX < 0){
			maxX.setText("Right");
		}
		if (deltaY > 0){
			maxY.setText("Down");
			//deltaY=0;
		}
		else if(deltaY < 0){
			maxY.setText("Up");
		}
		if (deltaZ<2){
			deltaZ=0;
		}
		lastX=event.values[0];
		lastY=event.values[1];
		lastZ=event.values[2];
		
		
	}
	
	public void clear(){
		currentX.setText("0.0");
		currentY.setText("0.0");
		currentZ.setText("0.0");
	}
	public void displayValue(){
		currentX.setText(Float.toString(deltaX));
		currentY.setText(Float.toString(deltaY));
		currentZ.setText(Float.toString(deltaZ));
	}


    @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
		
	}
    
}



here is my code but it giving only left and down direction
Posted
Comments
Kornfeld Eliyahu Peter 18-Jan-15 3:18am    
If you know where left is, you know where right is! The same for bottom!
vickeysanlge 18-Jan-15 3:21am    
I don't know.. I am new to android programing so tell me how can I do that?
any example..
Kornfeld Eliyahu Peter 18-Jan-15 3:25am    
Try removing Math.abs!? It will always return positive numbers...
vickeysanlge 18-Jan-15 3:35am    
Thanks man... I didn't realize that the math function is the cause
Kornfeld Eliyahu Peter 18-Jan-15 3:36am    
As I told...If you know where left is, you know where right is...
Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900