Activate proximity sensor on button click

1.7k views Asked by At

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

3

There are 3 answers

5
Andrea Cinesi On

you have to assign the onClick Listener in the onCreateView () method

start.setOnClickListener(  startCounter);
3
Skizo-ozᴉʞS ツ On

Add the onClickListener like this start.setOnClickListener(startCounter)

Or also you can do it by this way

bStart.setOnClickListener(new View.OnClickListener() {
@Override
   public void onClick(View v) {
    startCounter(v);
   }
});

EDIT

Let's make a change on your code to see if it's about your code or not.

First of all delete this line of code :

android:onClick="startCounter"

Then on your PushUp_Counter class change the header to this :

public class PushUp_Counter extends Activity implements SensorEventListener, OnClickListener  {

You have to add on your onCreate() this line of code :

start.setOnClickListener(this);

Then you have to add the method onClick() that looks like :

@Override
public void onClick(View v) { 
    // getId() returns this view's identifier.
    switch(v.getId()){
            case R.id.bStart;
              // 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);
           break;
    }
}

Comment the rest of code if it gives you any error, just try this button and let me know if it works.

0
ShrutiS On

The sensor listener seems to work when registered in the onResume()method. What i have done is let the sensor listener be activated but not start the counter until the button is clicked. As soon as the button is clicked, a boolean variable turns true, which is checked in onSensorChanged().

The xml:

<?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" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fitgo.Frag_Pushup"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />

    <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:onClick="startCounter"
            android:typeface="serif"
            
            
            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>

The java file:

public class PushUp_Counter extends Activity implements SensorEventListener {
 private SensorManager smgr;
 private Sensor sensor;
 Button start,stop,save;
 TextView title,count;
 int counter=0;
 boolean check;
@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);
 check=false;
 smgr=(SensorManager)getSystemService(SENSOR_SERVICE);
 sensor=smgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);

 
 stop.setEnabled(false);
 save.setEnabled(false);

 
}


@Override
protected void onResume() {
 // TODO Auto-generated method stub
 super.onResume();
 smgr.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL); 
 
}

@Override
protected void onPause() {
 // TODO Auto-generated method stub
 super.onPause();
 smgr.unregisterListener(this);
 
}

public void startCounter(View v) {
 // TODO Auto-generated method stub
 check=true;
 
 stop.setEnabled(true);
}


public void stopCounter(View v) {
 // TODO Auto-generated method stub
 check=false;
 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(check==true)
 {
 if (arg0.values[0] == 0){
  counter++;
  count.setText("Your count is: "+counter);
 }
 }
}}

I hope this would help someone like me.