Last active
August 29, 2015 14:20
-
-
Save shemul/5f3c7e8d6d1dc85da57d to your computer and use it in GitHub Desktop.
sensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sensor.example; | |
import java.util.List; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.hardware.Sensor; | |
import android.hardware.SensorEvent; | |
import android.hardware.SensorEventListener; | |
import android.hardware.SensorManager; | |
import android.media.AudioManager; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.TextView; | |
public class SensorExampleActivity extends Activity implements SensorEventListener { | |
private SensorManager senSensorManager; | |
private Sensor senAccelerometer; | |
private Sensor mprox; | |
TextView acc ; | |
TextView prox ; | |
float accX; | |
float accY; | |
float accZ; | |
private AudioManager myAudioManager; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); | |
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | |
mprox = senSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); | |
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL); | |
senSensorManager.registerListener(this, mprox, SensorManager.SENSOR_DELAY_NORMAL); | |
acc = (TextView)findViewById(R.id.textView1); | |
prox = (TextView)findViewById(R.id.textView2); | |
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); | |
} | |
public void onAccuracyChanged(Sensor arg0, int arg1) { | |
// TODO Auto-generated method stub | |
} | |
public void onSensorChanged(SensorEvent sensorEvent) { | |
// TODO Auto-generated method stub | |
//Sensor mySensor = sensorEvent.sensor; | |
Log.v("test","Test"); | |
if (sensorEvent.sensor == senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)) | |
{ | |
accX = sensorEvent.values[0]; | |
accY = sensorEvent.values[1]; | |
accZ = sensorEvent.values[2]; | |
acc.setText("ACC is : " + "X :" + accX + " Y :" + accY + " Z:" + accZ); | |
} | |
if (sensorEvent.sensor == senSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)) | |
{ | |
float val4 = sensorEvent.values[0]; | |
prox.setText("PRox is : " + val4); | |
if (val4 <= 0) | |
{ | |
if (accX > 2 && accY < -4 ) | |
{ | |
vibrate(null); | |
} | |
} else { | |
//if( myAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){ | |
//ring(); | |
//} | |
} | |
} | |
} | |
protected void onPause() { | |
super.onPause(); | |
senSensorManager.unregisterListener(this); | |
} | |
protected void onResume() { | |
super.onResume(); | |
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL); | |
senSensorManager.registerListener(this, mprox, SensorManager.SENSOR_DELAY_NORMAL); | |
} | |
public void vibrate(View view){ | |
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); | |
} | |
public void ring(){ | |
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment