I am trying to register the proximity sensor listener only when a button is clicked. But nothing seems to happen. I ahve tried exploring the internet for something of the sort but could not get anything. What am I doing wrong? Please help!
The xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pushupbg"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTitlePU"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Push Up Counter"
android:textColor="#98f5ff"
android:textSize="25sp"
android:padding="10sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/fragment1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<Button
android:id="@+id/bStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:typeface="serif"
android:onClick="startCounter"
android:text="Start" />
<Button
android:id="@+id/bStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:typeface="serif"
android:onClick="stopCounter"
android:text="Stop" />
<Button
android:id="@+id/bSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:typeface="serif"
android:onClick="saveData"
android:text="Save" />
</LinearLayout>
<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitlePU"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text=""
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
And here is the java file:
public class PushUp_Counter extends Activity implements SensorEventListener {
SensorManager smgr;
Sensor sensor;
Button start,stop,save;
TextView title,count;
int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pushup_layout);
start=(Button)findViewById(R.id.bStart);
stop=(Button)findViewById(R.id.bStop);
save=(Button)findViewById(R.id.bSave);
title=(TextView)findViewById(R.id.tvTitlePU);
count=(TextView)findViewById(R.id.tvCount);
stop.setEnabled(false);
save.setEnabled(false);
}
public void startCounter(View v) {
// TODO Auto-generated method stub
smgr=(SensorManager)getSystemService(SENSOR_SERVICE);
sensor=smgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);
smgr.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);
stop.setEnabled(true);
}
public void stopCounter(View v) {
// TODO Auto-generated method stub
smgr.unregisterListener(this);
save.setEnabled(true);
}
public void saveData(View v) {
// TODO Auto-generated method stub
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent arg0) {
// TODO Auto-generated method stub
if (arg0.values[0] == 0){
counter++;
count.setText("Your count is: "+counter);
}
}
}
Logcat:
06-19 00:01:21.780: W/EGL_genymotion(1493): eglSurfaceAttrib not implemented
06-19 00:01:29.396: I/ActivityManager(401): START u0 {cmp=com.example.fitgo/.PushUp_Counter} from pid 1493
06-19 00:01:29.412: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219
06-19 00:01:29.452: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219
06-19 00:01:29.472: W/genymotion_audio(125): out_write() limiting sleep time 67369 to 23219
06-19 00:01:29.504: W/genymotion_audio(125): out_write() limiting sleep time 58979 to 23219
06-19 00:01:29.524: W/genymotion_audio(125): out_write() limiting sleep time 40589 to 23219
06-19 00:01:29.548: W/genymotion_audio(125): out_write() limiting sleep time 32199 to 23219
06-19 00:01:29.572: W/genymotion_audio(125): out_write() limiting sleep time 23809 to 23219
06-19 00:01:29.856: D/dalvikvm(401): GC_FOR_ALLOC freed 441K, 17% free 13865K/16596K, paused 25ms, total 25ms
06-19 00:01:29.884: D/dalvikvm(401): GC_FOR_ALLOC freed 748K, 19% free 13487K/16596K, paused 22ms, total 22ms
06-19 00:01:29.884: D/MobileDataStateTracker(401): default: setPolicyDataEnable(enabled=true)
06-19 00:01:30.152: W/EGL_genymotion(1493): eglSurfaceAttrib not implemented
06-19 00:01:30.360: I/ActivityManager(401): Displayed com.example.fitgo/.PushUp_Counter: +458ms
06-19 00:01:34.372: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219
06-19 00:01:34.396: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219
06-19 00:01:34.424: W/genymotion_audio(125): out_write() limiting sleep time 47369 to 23219
06-19 00:01:34.448: W/genymotion_audio(125): out_write() limiting sleep time 38979 to 23219
06-19 00:01:34.492: W/genymotion_audio(125): out_write() limiting sleep time 31178 to 23219
06-19 00:01:38.448: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219
06-19 00:01:38.468: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219
06-19 00:01:38.496: W/genymotion_audio(125): out_write() limiting sleep time 47369 to 23219
06-19 00:01:38.516: W/genymotion_audio(125): out_write() limiting sleep time 38979 to 23219
06-19 00:01:38.564: W/genymotion_audio(125): out_write() limiting sleep time 31178 to 23219
you have to assign the onClick Listener in the onCreateView () method