Getting 0 as return values from accelrometer - android

I am having problem with this accelerometer coding. I keep getting 0 as my return value from the accelerometer. Just ignore the cal_displace part first because now i cant get any value form the accelerometer. your help is much appreciated.
package com.example.suntracking;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Accelerometer extends Activity implements SensorEventListener{
Sensor accelerometer;
SensorManager sm;
int samintval;
float ax,ay,axtemp ,aytemp ,sx,sy;
TextView showax,showay,showaxtemp,showaytemp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.accelerometer);
loadsavedPref();
initialize();
cal_displace();
display();
savePreferences();
}
private void display() {
// TODO Auto-generated method stub
showax.setText(""+ax);
showay.setText(""+ay);
showaxtemp.setText(""+axtemp);
showaytemp.setText(""+aytemp);
}
#Override
protected void onResume()
{
super.onResume();
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
showax=(TextView)findViewById(R.id.tvax);
showay=(TextView)findViewById(R.id.tvay);
showaxtemp=(TextView)findViewById(R.id.tvaxtemp);
showaytemp=(TextView)findViewById(R.id.tvaytemp);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
private void loadsavedPref() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
samintval = savedata.getInt("samintval", 1000);
}
private void cal_displace() {
// TODO Auto-generated method stub
sx = (float) (0.5 * ((ax - axtemp)/(samintval/1000)) * (samintval / 1000)*(samintval / 1000));
sy = (float) (0.5 * ((ay - aytemp)/(samintval/1000)) * (samintval / 1000)*(samintval / 1000));
//axtemp = ax;
//aytemp = ay;
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
ax = event.values[0];
ay = event.values[1];
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putFloat("ax", ax);
editor.putFloat("ay", ay);
editor.putFloat("axtemp", axtemp);
editor.putFloat("aytemp", aytemp);
editor.commit();
}
}

You're registering sensor listener twice, in initialize() and in onResume() - do it in onResume() only.
You're getting the values, but not updating your TextViews. Modify your onSensorChanged():
#Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
ax = event.values[0];
ay = event.values[1];
//update TextViews
display();
}

Related

dynamic communication between fragments Fail

I am trying to communicate dynamically between two fragments. Frag1 imlements sensorlistener and passes the values to the mainActivity.
the mainActivity implements onSendListener.onSendValues interface. in the implementation of onSendValues in the mainActivity, when the user clicks the button defined in Frag1, I pass the values of the Accelerometer (x,y,z) received from Frag1 to the Frag2 through the mainActivityby accessing Frag2's public methods
void returnedX(float x) {...}
void returnedY(float y) {...}
void returnedZ(float z) {...}
what happens is, when the user clicks the button in Frag1, the textViews in Frag2 displays nothing despite their public methods
void returnedX(float x) {...}
void returnedY(float y) {...}
void returnedZ(float z) {...}
are called, i am sure they were called because as shown below in the code, the Log statements in the public methods in Frag2 is displayed but the values of the acceöerometer not.
please see below the code, and let me know why the public methods in Frag2 do not display the passed date from Frag1
MainActivity:
public class MainActivity extends Activity implements onSendListener {
Frag2 frag2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
frag2 = (Frag2)getFragmentManager().findFragmentById (R.id.frag_2);
}
/**
* to send values from frag1 to frag2 through mainActivity. use bundle.
*/
#Override
public void onSendValues(float x, float y, float z) {
// TODO Auto-generated method stub
frag2.returnedX(x);
frag2.returnedY(y);
frag2.returnedZ(z);
}
}
Frag1:
public class Frag1 extends Fragment implements SensorEventListener {
private SensorManager sensorManager;
TextView tvAccX;
TextView tvAccY;
TextView tvAccZ;
private float x = 0.0f;
private float y = 0.0f;
private float z = 0.0f;
private void setAccX(float x) {
this.x = x;
}
private float getAccX() {
return this.x;
}
private void setAccY(float y) {
this.y = y;
}
private float getAccY() {
return this.y;
}
private void setAccZ(float z) {
this.z = z;
}
private float getAccZ() {
return this.z;
}
Button btnSend;
onSendListener sendValues;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
sendValues = (onSendListener) activity;
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_1, container, false);
tvAccX = (TextView) view.findViewById(R.id.tv_accX_value);
tvAccY = (TextView) view.findViewById(R.id.tv_accY_value);
tvAccZ = (TextView) view.findViewById(R.id.tv_accZ_value);
btnSend = (Button) view.findViewById(R.id.btn_send);
return view;
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
showAccReadings(event);
break;
default:
break;
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
private void showAccReadings(SensorEvent event) {
// TODO Auto-generated method stub
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
setAccX(x);
setAccY(y);
setAccZ(z);
tvAccX.setText(String.valueOf(x));
tvAccY.setText(String.valueOf(y));
tvAccZ.setText(String.valueOf(z));
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Sensor accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accSensor, sensorManager.SENSOR_DELAY_FASTEST);
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
sendValues.onSendValues(getAccX(), getAccY(), getAccZ());
}
});
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
sensorManager.unregisterListener(this);
}
}
Frag2:
public class Frag2 extends Fragment {
TextView tvX;
TextView tvY;
TextView tvZ;
float returnX ;
float returnY ;
float returnZ ;
void returnedX(float x) {
if (getView() != null) {
Log.d("Frag2", "X: View is not null");
this.returnX = x;
}
}
void returnedY(float y) {
if (getView() != null) {
Log.d("Frag2", "Y: View is not null");
this.returnY = y;
}
}
void returnedZ(float z) {
if (getView() != null) {
Log.d("Frag2", "Z: View is not null");
this.returnZ = z;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_2, container, false);
tvX = (TextView) view.findViewById(R.id.tv_accX2_label);
tvY = (TextView) view.findViewById(R.id.tv_accY2_label);
tvZ = (TextView) view.findViewById(R.id.tv_accZ2_label);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
Log.d("Frag2", "View is not null");
tvX.setText("" + this.returnX);
tvY.setText("" + this.returnY);
tvZ.setText("" + this.returnZ);
}
}
Log Output:
01-26 13:03:36.876: D/Frag2(16146): X: View is not null
01-26 13:03:36.876: D/Frag2(16146): Y: View is not null
01-26 13:03:36.876: D/Frag2(16146): Z: View is not null
You need to set the actual value to the TextViews
void returnedX(float x) {
if (tvX != null) {
tvX.setText("" + x);
}
}

Getting pitch and roll value

I am doing a project which is detecting the direction of moving by using accelerometer However, the 3 axis of accelerometer will not always follow the world xyz plane as we tilt or rotate our phone. I was trying to fix it by getting the pitch and roll from the phone and convert the axis of the phone to the world xyz axis. But now my pitch and roll always get 0. Can anyone help me ? or is there any other way to solve my problem? Thanks for the help!
package com.example.suntracking;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class AxisCheck extends Activity implements SensorEventListener{
Sensor accelerometer,magnetometer;
float[] lastAccelerometer = new float[3];
float[] lastMagnetometer = new float[3];
boolean lastAccelerometerSet = false;
boolean lastMagnetometerSet = false;
SensorManager sm;
float pval,rval;
float[] mR = new float[9];
float[] mhold= new float[3];
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initialize();
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
finish();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
sm.unregisterListener(this);
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
private void initialize() {
// TODO Auto-generated method stub
sm =(SensorManager)getSystemService(SENSOR_SERVICE);
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor == accelerometer) {
System.arraycopy(event.values, 0, lastAccelerometer, 0, event.values.length);
lastAccelerometerSet = true;
} else if (event.sensor == magnetometer) {
System.arraycopy(event.values, 0, lastMagnetometer, 0, event.values.length);
lastMagnetometerSet = true;
}
if (lastAccelerometerSet && lastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, lastAccelerometer, lastMagnetometer);
SensorManager.getOrientation(mR,mhold);
pval = Math.round(mhold[1]);
rval = Math.round(mhold[2]);
savePreferences();
}
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putFloat("pval", pval);
editor.putFloat("rval", rval);
editor.commit();
}
}

How to flush previous input on resuming activity in android?

I have this app where when i enter some values in editText of 1st activity i.e ActivityA and then press a Button shows the result in next activity i.e ActivityB. But when i revert back to ActivityA and change some values and press button so that i want another ActivityC to be called, ActivityB only appears.
but if i restart my application and put values required to get ActivityC i will get it. how am i supposed solve this problem?
EDIT :
ActivityA
package com.example.iolcalci;
import java.text.DecimalFormat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Selection extends Activity implements OnFocusChangeListener{
private EditText k1_e,k2_e,al_e,alconst_e,dr_e;
TextView k1_m,k2_m;
private float k1,k2,al,al_const,dr,Avg_k,v_k1,v_k2,v_dr;
private Float srkt_rnd,srk2_rnd,holladay_rnd,binkhorst_rnd,IOLPower_srkt,IOLPower_srk2,IOLPower_bink,IOLPower_holl;
private float srkt_rf2,srkt_rf3,srkt_rf4,srkt_rf5,srkt_rf6,srkt_rf7,srkt_rf8,srkt_rf9,srkt_rf10,srkt_rf11,srkt_rf12;
private float holladay_rf2,holladay_rf3,holladay_rf4,holladay_rf5,holladay_rf6,holladay_rf7,holladay_rf8,holladay_rf9,holladay_rf10,holladay_rf11,holladay_rf12;
private float binkhorst_rf2,binkhorst_rf3,binkhorst_rf4,binkhorst_rf5,binkhorst_rf6,binkhorst_rf7,binkhorst_rf8,binkhorst_rf9,binkhorst_rf10,binkhorst_rf11,binkhorst_rf12;
private Spinner spin;
private Button result;
private int spinSelected=-1;
int flag = 0;
String k1_s,k2_s,al_s,al_const_s,dr_s;
TextWatcher k1e,k2e,ale,dre;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selective);
setupUI(findViewById(R.id.form_layout));
spin=(Spinner)findViewById(R.id.formulae);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
// TODO Auto-generated method stub
spinSelected=pos;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
spinSelected=-1;
}
});
dr_e=(EditText)findViewById(R.id.dr_editText);
k1_e=(EditText)findViewById(R.id.k1_editText);
k2_e=(EditText)findViewById(R.id.K2_editText);
al_e=(EditText)findViewById(R.id.al_editText);
alconst_e=(EditText)findViewById(R.id.al_const_editText);
k1_m=(TextView)findViewById(R.id.k1_metric);
k2_m=(TextView)findViewById(R.id.k2_metric);
dr_e.setText("0.0");
dr_e.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
dr_s=dr_e.getText().toString();
v_dr=Float.valueOf(dr_s);
}
});
al_e.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
al_s = al_e.getText().toString();
int dotPos = -1;
for (int i = 0; i < al_s.length(); i++) {
char c = al_s.charAt(i);
if (c == '.') {
dotPos = i;
}
}
if (dotPos == -1){
if(al_s.length()==0){
al_e.setText("0.00");
}else{
al_e.setText(al_s + ".00");
}
} else {
if ( al_s.length() - dotPos == 1 ) {
al_e.setText(al_s + "00");
} else if ( al_s.length() - dotPos == 2 ) {
al_e.setText(al_s + "0");
}
}
}
}
});
k1_e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
String k1_s = k1_e.getText().toString();
int dotPos = -1;
for (int i = 0; i < k1_s.length(); i++) {
char c = k1_s.charAt(i);
if (c == '.') {
dotPos = i;
}
}
if (dotPos == -1){
if(k1_s.length()==0){
k1_e.setText("0.00");
}else{
k1_e.setText(k1_s + ".00");
}
} else {
if ( k1_s.length() - dotPos == 1 ) {
k1_e.setText(k1_s + "00");
} else if ( k1_s.length() - dotPos == 2 ) {
k1_e.setText(k1_s + "0");
}
}
}
k1_s=k1_e.getText().toString().trim();
k1_e.setText(String.format(k1_s,"##.00"));
if(k1_s.length()!=0){
k1=Float.parseFloat(k1_s);
v_k1=Float.valueOf(k1);
Log.v("K1",""+v_k1);
if((v_k1>=(float)5.63)&&(v_k1<=(float)11.25)){
k1_m.setText("mm");
Log.v("CHECK1", "success1" +v_k1);
}else if((v_k1>=(float)30)&&(v_k1<=(float)60)){
k1_m.setText("D");
Log.v("CHECK1", "success1" +v_k1);
}else{
}
}
}
});
k2_e.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus){
String k2_s = k2_e.getText().toString();
int dotPos = -1;
for (int i = 0; i < k2_s.length(); i++) {
char c = k2_s.charAt(i);
if (c == '.') {
dotPos = i;
}
}
if (dotPos == -1){
if(k2_s.length()==0){
k2_e.setText("0.00");
}else{
k2_e.setText(k2_s + ".00");
}
} else {
if ( k2_s.length() - dotPos == 1 ) {
k2_e.setText(k2_s + "00");
} else if ( k2_s.length() - dotPos == 2 ) {
k2_e.setText(k2_s + "0");
}
}
}
k2_s=k2_e.getText().toString().trim();
if(k2_s.length()!=0){
k2=Float.parseFloat(k2_s);
v_k2=Float.valueOf(k2);
Log.v("K2",""+v_k2);
if((v_k2>=5.63)&&(v_k2<=11.25)){
k2_m.setText("mm");
Log.v("CHECK2", "success2" +v_k2);
}else if((v_k2>=30)&&(v_k1<=60)){
k2_m.setText("D");
Log.v("CHECK2", "success2" +v_k2);
}else{
}
}
}
});
result=(Button)findViewById(R.id.result);
result.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(dr_e.getText().toString().length()==0|al_e.getText().toString().length()==0|alconst_e.getText().toString().length()==0){
flag=1;
}else{
dr=Float.parseFloat(dr_e.getText().toString());
al=Float.parseFloat(al_e.getText().toString());
al_const=Float.parseFloat(alconst_e.getText().toString());
k1=Float.parseFloat(k1_e.getText().toString().trim());
k2=Float.parseFloat(k2_e.getText().toString().trim());
}
if((v_k1>=(float)5.63)&&(v_k1<=(float)11.25)){
k1=Round((float)(337.5/v_k1),2);
}
if((v_k2>=(float)5.63)&&(v_k2<=(float)11.25)){
k2=Round((float)(337.5/v_k2),2);
}
Avg_k=(k1+k2)/2;
if(spinSelected==0){
IOLPower_srkt=Srkt();
if(flag!=1){
Intent iSrkt=new Intent(Selection.this,Srkt.class);
iSrkt.putExtra("RESULT", IOLPower_srkt);
iSrkt.putExtra("DR", dr);
iSrkt.putExtra("IOL", srkt_rnd);
iSrkt.putExtra("REFER7", srkt_rf7);
iSrkt.putExtra("REFER2", srkt_rf2);
iSrkt.putExtra("REFER3", srkt_rf3);
iSrkt.putExtra("REFER4", srkt_rf4);
iSrkt.putExtra("REFER5", srkt_rf5);
iSrkt.putExtra("REFER6", srkt_rf6);
iSrkt.putExtra("REFER8", srkt_rf8);
iSrkt.putExtra("REFER9", srkt_rf9);
iSrkt.putExtra("REFER10", srkt_rf10);
iSrkt.putExtra("REFER11", srkt_rf11);
iSrkt.putExtra("REFER12", srkt_rf12);
startActivity(iSrkt);
}else{
Intent iSrktx=new Intent(Selection.this,Srkt_x.class);
startActivity(iSrktx);
}
}else if(spinSelected==1){
IOLPower_bink=Binkhorst();
if(flag!=1){
Intent iBinkhorst=new Intent(Selection.this,Binkhorst.class);
iBinkhorst.putExtra("RESULT", IOLPower_bink);
iBinkhorst.putExtra("DR", dr);
iBinkhorst.putExtra("IOL", binkhorst_rnd);
iBinkhorst.putExtra("REFER7", binkhorst_rf7);
iBinkhorst.putExtra("REFER2", binkhorst_rf2);
iBinkhorst.putExtra("REFER3", binkhorst_rf3);
iBinkhorst.putExtra("REFER4", binkhorst_rf4);
iBinkhorst.putExtra("REFER5", binkhorst_rf5);
iBinkhorst.putExtra("REFER6", binkhorst_rf6);
iBinkhorst.putExtra("REFER8", binkhorst_rf8);
iBinkhorst.putExtra("REFER9", binkhorst_rf9);
iBinkhorst.putExtra("REFER10", binkhorst_rf10);
iBinkhorst.putExtra("REFER11", binkhorst_rf11);
iBinkhorst.putExtra("REFER12", binkhorst_rf12);
startActivity(iBinkhorst);
}else{
Intent iBinkhorstx=new Intent(Selection.this,Binkhorst_x.class);
startActivity(iBinkhorstx);
}
}else if(spinSelected==2){
IOLPower_srk2=Srk2();
if((Math.ceil(IOLPower_srk2)-IOLPower_srk2)>0.5){
srk2_rnd=(float) Math.floor(IOLPower_srk2);
}else{
srk2_rnd=(float) Math.ceil(IOLPower_srk2);
}
if(flag!=1){
Intent iSrk2=new Intent(Selection.this,Srk2.class);
iSrk2.putExtra("RESULT", IOLPower_srk2);
iSrk2.putExtra("DR", dr);
iSrk2.putExtra("IOL", srk2_rnd);
startActivity(iSrk2);
}else{
Intent iSrk2x=new Intent(Selection.this,Srk2_x.class);
startActivity(iSrk2x);
}
}else{
IOLPower_holl=Holladay();
if(flag!=1){
Intent iHolladay=new Intent(Selection.this,Holladay.class);
iHolladay.putExtra("RESULT", IOLPower_holl);
iHolladay.putExtra("DR", dr);
iHolladay.putExtra("IOL", holladay_rnd);
iHolladay.putExtra("REFER7", holladay_rf7);
iHolladay.putExtra("REFER2", holladay_rf2);
iHolladay.putExtra("REFER3", holladay_rf3);
iHolladay.putExtra("REFER4", holladay_rf4);
iHolladay.putExtra("REFER5", holladay_rf5);
iHolladay.putExtra("REFER6", holladay_rf6);
iHolladay.putExtra("REFER8", holladay_rf8);
iHolladay.putExtra("REFER9", holladay_rf9);
iHolladay.putExtra("REFER10", holladay_rf10);
iHolladay.putExtra("REFER11", holladay_rf11);
iHolladay.putExtra("REFER12", holladay_rf12);
startActivity(iHolladay);
}else{
Intent iHolladayx=new Intent(Selection.this,Holladay_x.class);
startActivity(iHolladayx);
}
}
}
});
Button reset=(Button) findViewById(R.id.reset);
reset.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dr_e.setText("0.0");
al_e.setText("");
k1_e.setText("");
k2_e.setText("");
alconst_e.setText("");
dr_e.requestFocus();
}
});
}
public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
public float Srkt(){
float Rcor;
float Lcor;
float Crwdest;
float Corneal_H;
float Acd_Const;
float Offset;
float Acd_Est;
float Na=(float) 1.336;
float C2=(float) 0.3333;
float C3;
float C4;
float C5;
float C6;
float C8;
float C9;
float Iolam;
float Rcor1=(float)(337.5/Avg_k);Rcor=Round(Rcor1,2);
if(al<=24.2){
Lcor=al;
}else{
Lcor=(float) (-3.446+1.716*al-0.0237*(al*al));
}Lcor=Round(Lcor,2);
Crwdest=(float) (-5.41+0.58412*Lcor+0.098*Avg_k);Crwdest=Round(Crwdest,2);
Corneal_H=(float) (Rcor-(Math.sqrt(Rcor*Rcor-Crwdest*Crwdest/4)));Corneal_H=Round(Corneal_H,2);
Acd_Const=(float) (0.62467*al_const-68.747);
Offset=(float) (Acd_Const-3.336);Offset=Round(Offset,2);
Acd_Est=(Corneal_H+Offset); float Acd_Est1=Round(Acd_Est,2);
C3=(float) (0.97971*al+0.65696);C3=Round(C3,2);
C4=C3-Acd_Est1;C4=Round(C4,2);
C5=(float) ((Na*Rcor)-(C2*Acd_Est)); C5=Round(C5, 2);
C6=(float) ((Na*Rcor1)-(C2*C3));
C8=(float) ((12*C6)+(C3*Rcor1));C8=Round(C8,2);
C9=(float) ((12*C5)+(Acd_Est*Rcor1));C9=Round(C9,2);
Iolam=(float) ((1336*(C6-(0.001*C8*dr)))/(C4*(C5-(0.001*dr*C9))));Iolam=Round(Iolam,2);
if((Math.ceil(Iolam)-Iolam)>0.5){
srkt_rnd=(float) Math.floor(Iolam);
}else{
srkt_rnd=(float) Math.ceil(Iolam);
}
srkt_rf7=(float) (((1336*C6)-(srkt_rnd*C4*C5))/((1.336*C8)-(0.001*srkt_rnd*C4*C9)));
srkt_rf6=(float) (((1336*C6)-((srkt_rnd+0.5)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd+0.5)*C4*C9)));
srkt_rf5=(float) (((1336*C6)-((srkt_rnd+1)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd+1)*C4*C9)));
srkt_rf4=(float) (((1336*C6)-((srkt_rnd+1.5)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd+1.5)*C4*C9)));
srkt_rf3=(float) (((1336*C6)-((srkt_rnd+2)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd+2)*C4*C9)));
srkt_rf2=(float) (((1336*C6)-((srkt_rnd+2.5)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd+2.5)*C4*C9)));
srkt_rf8=(float) (((1336*C6)-((srkt_rnd-0.5)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd-0.5)*C4*C9)));
srkt_rf9=(float) (((1336*C6)-((srkt_rnd-1)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd-1)*C4*C9)));
srkt_rf10=(float) (((1336*C6)-((srkt_rnd-1.5)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd-1.5)*C4*C9)));
srkt_rf11=(float) (((1336*C6)-((srkt_rnd-2)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd-2)*C4*C9)));
srkt_rf12=(float) (((1336*C6)-((srkt_rnd-2.5)*C4*C5))/((1.336*C8)-(0.001*(srkt_rnd-2.5)*C4*C9)));
return(Iolam);
}
public float Binkhorst(){
float K1;
float LB2;
float ACDbnk;
float xb;
float yb;
float em;
K1=(float)(337.5/Avg_k);
LB2=(float) (al+0.1984);
if(LB2>=26){
ACDbnk=(float) (((0.58357*al_const)-63.896)*1.1087);
}else{
ACDbnk=(float) (((0.58357*al_const)-63.896)*LB2/23.45);
}
xb=(float) (1336*((1.336*K1-0.3333*LB2)-0.001*dr*(16.032*K1-4*LB2+LB2*K1)));
yb=(float) ((LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk-0.001*dr*(16.032*K1-4*ACDbnk+ACDbnk*K1)));
em=xb/yb;em=Round(em,2);
if((Math.ceil(em)-em)>0.5){
binkhorst_rnd=(float) Math.floor(em);
}else{
binkhorst_rnd=(float) Math.ceil(em);
}
binkhorst_rf7=(float) ((1336*((1.336*K1-0.3333*LB2))-binkhorst_rnd*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*binkhorst_rnd*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf6=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd+0.5)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd+0.5)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf5=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd+1)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd+1)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf4=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd+1.5)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd+1.5)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf3=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd+2)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd+2)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf2=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd+2.5)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd+2.5)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf8=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd-0.5)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd-0.5)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf9=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd-1)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd-1)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf10=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd-1.5)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd-1.5)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf11=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd-2)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd-2)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
binkhorst_rf12=(float) ((1336*((1.336*K1-0.3333*LB2))-(binkhorst_rnd-2.5)*(LB2-ACDbnk)*(1.336*K1-0.3333*ACDbnk))/(1.336*((16.032*K1)-(4*LB2)+(LB2*K1))-0.001*(binkhorst_rnd-2.5)*(LB2-ACDbnk)*((16.032*K1)-(4*ACDbnk)+(ACDbnk*K1))));
return(em);
}
public float Srk2(){
float X25 = 0;
float Avg_k=(k1+k2)/2;
if(al<20.0){
X25=al_const+3;
}else if((al>=20.0)&&(al<21.0)){
X25=al_const+2;
}else if((al>=21.0)&&(al<22.0)){
X25=al_const+1;
}else if((al>=22.0)&&(al<24.5)){
X25=al_const;
}else if(al>=24.5){
X25=(float) (al_const-(0.5));
}else{
Toast.makeText(getApplicationContext(), "Please enter valid AL value", Toast.LENGTH_SHORT).show();
}
float SRK2 = (float) (X25-(0.9*Avg_k+2.5*al));
return(SRK2);
}
public float Holladay(){
float K;
float Lhol;
float SF;
float Rag;
float AGx;
float AG;
float ACDH;
float CAhol;
float xh;
float yh;
float K1=(float)(337.5/Avg_k);K=Round(K1,2);
Lhol=(float) (al+0.2);
SF=(float) ((0.5663*al_const)-65.6);
if(K<7){
Rag=7;
}else{
Rag=K;
}
AGx=(float) (al*0.533);
if(AGx>13.5){
AG=(float) 13.5;
}else{
AG=AGx;
}
ACDH=(float) (0.56+Rag-(Math.sqrt(Rag*Rag-AG*AG/4)));
CAhol=ACDH+SF;
xh=(float) (1336*((1.336*K1-0.3333*Lhol)-0.001*dr*(16.032*K1-4*Lhol+Lhol*K1)));
yh=(float) ((Lhol-CAhol)*(1.336*K1-0.3333*CAhol-0.001*dr*(16.032*K1-4*CAhol+CAhol*K1)));
float em=xh/yh;em=Round(em,2);
if((Math.ceil(em)-em)>0.5){
holladay_rnd=(float) Math.floor(em);
}else{
holladay_rnd=(float) Math.ceil(em);
}
holladay_rf7=(float) ((1336*((1.336*K1-0.3333*Lhol))-holladay_rnd*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*holladay_rnd*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf6=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd+0.5)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd+0.5)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf5=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd+1)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd+1)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf4=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd+1.5)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd+1.5)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf3=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd+2)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd+2)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf2=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd+2.5)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd+2.5)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf8=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd-0.5)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd-0.5)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf9=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd-1)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd-1)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf10=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd-1.5)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd-1.5)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf11=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd-2.0)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd-2)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
holladay_rf12=(float) ((1336*((1.336*K1-0.3333*Lhol))-(holladay_rnd-2.5)*(Lhol-CAhol)*(1.336*K1-0.3333*CAhol))/(1.336*((16.032*K1)-(4*Lhol)+(Lhol*K1))-0.001*(holladay_rnd-2.5)*(Lhol-CAhol)*((16.032*K1)-(4*CAhol)+(CAhol*K1))));
return(em);
}
//when the back button is pressed from next activity
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
dr_e.requestFocus();
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public void setupUI(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
hideSoftKeyboard(Selection.this);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
}
ActivityB
package com.example.iolcalci;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Srk2 extends Activity {
private Float Result,dr,Srk2_rnd7,Srk2_rnd2,Srk2_rnd3,Srk2_rnd4,Srk2_rnd5,Srk2_rnd6,Srk2_rnd8,Srk2_rnd9,Srk2_rnd10,Srk2_rnd11,Srk2_rnd12;
private Float Ref2,Ref3,Ref4,Ref5,Ref6,Ref7,Ref8,Ref9,Ref10,Ref11,Ref12;
private float Rf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_srk2);
Thread srk2=new Thread(){
public void run() {
TextView power=(TextView) findViewById(R.id.srk2_power);
TextView name=(TextView) findViewById(R.id.iolpower);
Result=getIntent().getExtras().getFloat("RESULT");
dr=getIntent().getExtras().getFloat("DR");
if(dr==0){
name.setText("EM :");
}else{
name.setText("AM :");
}
Srk2_rnd7=getIntent().getExtras().getFloat("IOL");
power.setText(String.valueOf(Result));
//some more calculation
}
};
srk2.start();
}
public float refraction(Float ans){
Rf=refer();
return((Result-ans)/Rf);
}
public float refer(){
float Refer;
if(Result>14){
Refer=(float) 1.25;
}else{
Refer=1;
}
return(Refer);
}
}
ActivityC
package com.example.iolcalci;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
#SuppressLint("NewApi")
public class Srk2_x extends Activity
{
Float result;
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_srk2_x);
TextView text = (TextView) findViewById(R.id.srk2_power);
text.setText("INVALID");
}
}
By maintaining visited activity Queue or use Activity stack to track the activity sequence.
As I can see the flag value is controlling which activity to start. I guess you should change flag to default value when you start a new activity, or override onPause method in your activity and change it there.

accelerometer in android

The following example is taken from internet. In all the example the code is explained using a seprate thread for sensor manager. Could you please let me know why all the accelerometer is explained with a seperate thread for SensorManager
package com.example.myapp1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;
public class Accelerate extends Activity implements SensorEventListener {
float x, y, sensorX, sensorY;
Bitmap ball;
SensorManager sm;
Sensor s;
MyBringBackSurface ourSurfaceView;
public class MyBringBackSurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MyBringBackSurface(Context context) {
super(context);
Log.i("Notice","In constructor of mybringbacksurface");
ourHolder = getHolder();
}
public void pause() {
isRunning = false;
Log.i("Notice","In pause of mybringbacksurface");
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
Log.i("Notice","In resume of mybringbacksurface");
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
public void run() {
// TODO Auto-generated method stub
Log.i("Notice","In run of mybringbacksurface");
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
float centerX = canvas.getWidth() / 2;
float centerY = canvas.getHeight() / 2;
canvas.drawBitmap(ball, centerX - sensorX * 18, centerY
+ sensorY * 18, null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("Notice","In oncreate of of Accelerator");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
ball = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
x = y = sensorX = sensorY = 0;
ourSurfaceView = new MyBringBackSurface(this);
ourSurfaceView.resume();
setContentView(ourSurfaceView);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
Log.i("Notice","In pause of Accelerator");
sm.unregisterListener(this, s);
super.onPause();
ourSurfaceView.pause();
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent e) {
// TODO Auto-generated method stub
Log.i("Notice","In sensorchanged of of Accelerator");
try {
Thread.sleep(32);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sensorX = e.values[0];
sensorY = e.values[1];
}
}
Because the continuous while loop in the run() method would block the main thread and cause your app to be shut down with an "Application Not Responding" or ANR.
Anytime you have a long running loop like that, it must happen in a separate thread.

Accelerometer code

I am working with accelerometer for android, I do not understand why the three axis for them which I have assigned to mSensorX, mSensorY and mSensorZ are labelled as unused when I have used them in the onSensorChange, could someone please help me understand this.
package com.example.imageaccel;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class ImageAccelActivity extends Activity implements SensorEventListener {
/** Called when the activity is first created. */
TextView x, y, z;
private float mSensorX;
private float mSensorY;
private float mSensorZ;
private Bitmap car;
private SensorManager sm = null;
// Bitmap car1;
// float x1, y1, z1, sensorX, sensorY, sensorZ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
x = (TextView)findViewById(R.id.x_axis);
y = (TextView)findViewById(R.id.y_axis);
z = (TextView)findViewById(R.id.z_axis);
SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() !=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
super.onPause();
}
#Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent ev) {
// TODO Auto-generated method stub
if(ev.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
mSensorX = ev.values[0];
mSensorY = ev.values[1];
mSensorZ = ev.values[2];
}
}
protected void onDraw(Canvas canvas) {
/*
* draw the background
*/
canvas.drawBitmap(car, 0, 0, null);
}
}
You're setting them to a value but that's only half the story. The complaint is most likely that you're never using them after that. It's similar to the C code:
int main (void) {
int a = 1;
return 0;
}
While that compiles and runs fine, you do get a warning (using gcc -Wall) that:
warning: unused variable 'a'
Quick way to check this theory, put a:
System.out.println (mSensorX + "," + mSensorY + "," + mSensorZ);
(or some other sort of use) following the setting and see if the warning disappears.
I think there is a warning because of the if statement there..In some cases they won't be used and hence the warning..That's my opinion:)

Categories

Resources