I am a java newer,
I want to get, for example, 10 different values of my phone's acceleration,
and get an average value from them.
But I can only find answers for continued data, or only one data.
Now I can get one data and display it on my screen.
Can I use for loop to calculate for 10 times?
Thank you.
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
aAngle = event.values[0];
bAngle = event.values[1];
}
private Button.OnClickListener listener = new Button.OnClickListener() {
switch(viewMode){
case VIEW_MODE_1:
title.setText(Double.toString(aAngle) + " " + Double.toString(bAngle));
break;}
I've tried to cast a for loop in the OnSensorChanged method, but it didn't work at all.
I tried to do this, but it seems to have a runtime error.
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0 && sensors.size() <= 10) {
sensorManager.registerListener(this,sensors.get(count),SensorManager.SENSOR_DELAY_NORMAL);
count ++;
}
also, I create two list to save the result every time when my angle is changed.
for(int i = 0; i < aAngleList.size(); i++){
double aAngleTotal = 0;
double bAngleTotal = 0;
aAngleTotal += aAngleList.get(i);
bAngleTotal += bAngleList.get(i);
aAngleAverage = aAngleTotal / aAngleList.size();
bAngleAverage = bAngleTotal / bAngleList.size();
}
public void onSensorChanged(SensorEvent event) {
aAngleList.add(aAngle);
bAngleList.add(bAngle);
}
the for loop is in onResume, right under the if (sensors...)
onSensorChanged is fired everytime your sensor gets a new reading. you can place a counter for ten changes. keep track of values in a list,hashmap and aggregate them.
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
private int count=0;
private List<String> eventX=new ArrayList<String>();
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
eventX.add(event.getValueMethodwhateverItis());
count++;
if (count=10){
//aggregate from list do anything you want.;
}
}
}
Related
I am in the process of developing an Android music player which will do the the following actions once a shake action is detected. Actions: 1. Do Nothing 2. Play or Pause 3. Next Song 4. Previous Song
I have created a Service class for the shaking action and it is as follows.
public class Shaker extends Service implements SensorEventListener {
SharedPreferences sharedPreferences;
private static final int MIN_TIME_BETWEEN_SHAKES_MILLISECS = 1000;
private long mLastShakeTime;
private SensorManager sensorManager;
private static final String SHAKE_ACTION_PREFERENCE = "shake_action_preference";
private static final String SHAKE_THRESHOLD_PREFERENCE = "shake_threshold_preference";
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
sensorManager.unregisterListener(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
return START_STICKY;
}
public void onStart(Intent intent, int startId) {
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
if ((curTime - mLastShakeTime) > MIN_TIME_BETWEEN_SHAKES_MILLISECS) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
double acceleration = Math.sqrt(Math.pow(x, 2) +
Math.pow(y, 2) +
Math.pow(z, 2)) - SensorManager.GRAVITY_EARTH;
Log.d("Acceleration", "Acceleration is " + acceleration + "m/s^2");
String threshold = sharedPreferences.getString(SHAKE_THRESHOLD_PREFERENCE, "medium");
float SHAKE_THRESHOLD;
if(threshold.equals("high"))
SHAKE_THRESHOLD = 40.5f;
else if(threshold.equals("low"))
SHAKE_THRESHOLD = 15.5f;
else
SHAKE_THRESHOLD = 25.5f;
if (acceleration > SHAKE_THRESHOLD) {
mLastShakeTime = curTime;
Log.d("Shake", "Shake, Rattle, and Roll");
String opt = sharedPreferences.getString(SHAKE_ACTION_PREFERENCE, "nothing");
Log.d("SharedPreference", opt);
Log.d("SharedPreference", threshold);
if(opt.equals("play")) {
MusicPlayer.playOrPause();
} else if(opt.equals("next")) {
MusicPlayer.next();
} else if(opt.equals("prev")) {
MusicPlayer.previous(getApplicationContext(), false);
}
}
}
}
}
}
But when I run the apk files in actual devices, the shaking function is working only on certain phones and not working for all the mobiles.
I have tried on different mobiles using same Android OS, but it is working in one and not working in the other.
I couldn't figure out why it is like this. Can anybody suggest a solution for this?
I had this problem. onSensorChanged method is called for any changes but in some devices, it is called more than once.It cause problem in logic of the code.
I solved this problem by adding a private static boolean variable. (It causes this method called once).Once this method executes,this variable becomes true.
#Override
public void onSensorChanged(SensorEvent event) {
if(!variable) {
variable=true;
...
...
variable=false;
}
}
If the user turns over the phone I want to react to that by stopping my text to speech reading. It would be a nice feature for my app, but how can I detect this motion? I'm not really familiar with motion sensors and I could not find this specific motion listener anywhere, mostly just screen orientations. Thanks for the help!
This sample activity demonstrates how a device's gravity sensor can be used to detect when the device is turned over. In method onSensorChanged(), term factor determines how complete the "turn over" must be. Typical range might be 0.7 to 0.95.
Support for Gravity Sensor was added in Android API 9. Not all devices have a gravity sensor.
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private static final String TAG = "Demo";
private SensorManager mSensorManager;
private Sensor mGravitySensor;
private boolean mFacingDown;
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// nothing
}
#Override
public void onSensorChanged(SensorEvent event) {
final float factor = 0.95F;
if (event.sensor == mGravitySensor) {
boolean nowDown = event.values[2] < -SensorManager.GRAVITY_EARTH * factor;
if (nowDown != mFacingDown) {
if (nowDown) {
Log.i(TAG, "DOWN");
} else {
Log.i(TAG, "UP");
}
mFacingDown = nowDown;
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mGravitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
if (mGravitySensor == null) {
Log.w(TAG, "Device has no gravity sensor");
}
}
#Override
protected void onResume() {
super.onResume();
if (mGravitySensor != null) {
mSensorManager.registerListener(this, mGravitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
I know that there are only two values of proximity : 0.0 & 1.0
I need a simple code to detect proximity through the device's sensors, and perform any task if proximity changes to 1.0
this is what i found.
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sensor_screen);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
iv = (ImageView) findViewById(R.id.imageView1);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 0) {
iv.setImageResource(R.drawable.near);
} else {
iv.setImageResource(R.drawable.far);
}
}
}
You can use the getMaximumRange() method to obtain your proximity sensor's max range and use it to determine what to do.
SensorManager sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
float maxRange = proximitySensor.getMaximumRange();
Now use maxRange to do what you want to do.
public void onSensorChanged(SensorEvent event) {
if(maxRange == event.values[0]) {
// Do something when something is far away.
}
else {
// Do something when something is near.
}
}
One very important thing. The proximity sensor varies from device to device. It not always reports 1.0 and 0.0. For example my Moto G (1st Gen.) reports 3 cm as its min range and 100 cm as its max range. So its best to obtain the max range of the device's sensor and use it in your code rather then hard-coding the value.
I have the following class to read data from accelerometer:
public class Accelerometer implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
float deltaX;
float deltaY;
float deltaZ;
Activity activity;
public Accelerometer(Activity act)
{
this.activity = act;
mSensorManager = (SensorManager) this.activity.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
public float getDeltaX()
{
return this.deltaX;
}
public float getDeltaY()
{
return this.deltaY;
}
public float getDeltaZ()
{
return this.deltaZ;
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
deltaX = event.values[0];
deltaY = event.values[1];
deltaZ = event.values[2];
}
}
and I am accessing this class from my main activity class using the following code:
Accelerometer sbt = new Accelerometer (this);
tvX.setText(Float.toString(sbt.getDeltaX()) + " " +Float.toString(sbt.getDeltaY()) + " "+Float.toString(sbt.getDeltaZ()));
However, it always shows me 0, 0, 0. not sure whats wrong with current code. any help would be highly appreciated.
THe accelerometer (and all sensor data) works on a callback system. It won't call you until the next time it reads the sensor, and it will continue to call your onSensorChanged until you unregister. But if you try to set the text immediately after registering, you won't have been called yet. You need to set the textView in onSensorChanged, when you're updated with new values.
I have seen a few code examples of 1.0 / 1.1 but since a lot of that is deprecated now I was hoping to find a nice 1.5+ example. I have some code here that I wrote but it doesn't really work properly. Any help would be great, thanks!
public class collectAccel extends Activity implements SensorEventListener,
OnClickListener{
private SensorManager sensorMgr;
private TextView xLabel, yLabel, zLabel;
private Button StartBtn;
private List<Sensor> sensorList;
private float x, y, z;
private long lastUpdate = -1;
// deltas for calibration
private float cx, cy, cz;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
xLabel = (TextView) findViewById(R.id.x_label);
yLabel = (TextView) findViewById(R.id.y_label);
zLabel = (TextView) findViewById(R.id.z_label);
StartBtn = (Button) findViewById(R.id.Button1);
StartBtn.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
if (xLabel.getVisibility() != 1)
xLabel.setVisibility(1);
if (yLabel.getVisibility() != 1)
yLabel.setVisibility(1);
if (zLabel.getVisibility() != 1)
zLabel.setVisibility(1);
}
#Override
protected void onPause() {
super.onPause();
sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
sensorMgr = null;
cx = 0;
cy = 0;
cz = 0;
}
#Override
protected void onResume() {
super.onResume();
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorList = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
boolean accelSupported = sensorMgr.registerListener((SensorEventListener)this,
sensorList.get(0),
SENSOR_DELAY_UI);
if (!accelSupported) {
// on accelerometer on this device
sensorMgr.unregisterListener((SensorEventListener)this, sensorList.get(0));
}
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
cx = 0;
cy = 0;
cz = 0;
}
#Override
public void onSensorChanged(SensorEvent arg0) {
if (arg0.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
// only allow one update every 100ms, otherwise updates
// come way too fast and the phone gets bogged down
// with garbage collection
if (lastUpdate == -1 || (curTime - lastUpdate) > 100) {
lastUpdate = curTime;
x = arg0.values[0];
y = arg0.values[1];
z = arg0.values[2];
xLabel.setText(String.format("X: %+2.5f (%+2.5f)", (x+cx), cx));
yLabel.setText(String.format("Y: %+2.5f (%+2.5f)", (y+cy), cy));
zLabel.setText(String.format("Z: %+2.5f (%+2.5f)", (z+cz), cz));
}
}
}
}
You can look it up in the Android Reference but It looks like you'll need
SensorManager mgr = Context.getSystemService(SENSOR_SERVICE);
IList<Sensor> sensorList = mgr.getSensorList(SensorManager.SENSOR_ACCELEROMETER);
If you really want the list, but I think you are actually meant to use SensorManager.RegisterListener(SensorListener listener, int sensors, int rate) or another version of that method.