Android Open Source - Android-Sessions Proximity Activity






From Project

Back to project page Android-Sessions.

License

The source code is released under:

MIT License

If you think the Android project Android-Sessions listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.ac.icappsoc.appsocthree.proximity;
/*w ww  . jav a  2s .  c  o  m*/
import uk.ac.icappsoc.appsocthree.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

// We implement SensorEventListener so we get notified when new sensor data is available.
public class ProximityActivity extends Activity implements SensorEventListener {

  /**
   *  A reference to the system SensorManager, which we use to access all the sensors we'll want.
   *  In our case, we'll use it to start and stop listening for sensor events, as well as retrieve
   *  references to the sensors we will use.
   */
  private SensorManager sensorManager;
  private Sensor proximitySensor;
  
  private float maxDistance;
  private TextView textView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_proximity);
    
    // Retrieve the SensorManager and get a reference to the sensor we want.
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    
    // Retrieve the maximum distance our proximity sensor can return.
    maxDistance = proximitySensor.getMaximumRange();
    
    // Grab our TextView so we can display our data to the world!
    textView = (TextView) findViewById(R.id.text);
  }
  
  // Note that onResume() is called every time an Activity comes to the foreground, including the first time it starts up.
  @Override
  protected void onResume(){
    super.onResume();
    // Register this Activity as the listener for our proximity sensor as soon as we resume running.
    sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_UI);
  }
  
  @Override
  protected void onPause(){
    super.onPause();
    // Unregister this Activity as soon as it's paused to prevent battery drain.
    sensorManager.unregisterListener(this);
  }
  
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Typically we're not too interested in doing anything here.
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    // Here's where the meat lies!
    if(event.sensor.getType() == Sensor.TYPE_PROXIMITY){
      // Aha! This is our proximity sensor.
      float distanceCm = event.values[0];
      
      if(maxDistance == distanceCm){
        textView.setText("Far");
        getWindow().getDecorView().setBackgroundColor(Color.argb(255, 237, 30, 36)); // app_soc_red
      } else if(distanceCm > 0){
        textView.setText(distanceCm + " cm away");
        getWindow().getDecorView().setBackgroundColor(Color.argb(255, 164, 198, 57)); // app_soc_green
      } else if(0 == distanceCm){
        textView.setText("Near");
        getWindow().getDecorView().setBackgroundColor(Color.argb(255, 164, 198, 57)); // app_soc_green
      }
    }
    
  }

}




Java Source Code List

uk.ac.icappsoc.appsocone.MainActivity.java
uk.ac.icappsoc.appsocthree.MainActivity.java
uk.ac.icappsoc.appsocthree.SensorListActivity.java
uk.ac.icappsoc.appsocthree.accel.BouncyBallView.java
uk.ac.icappsoc.appsocthree.accel.Gravity2DActivity.java
uk.ac.icappsoc.appsocthree.accel.ShakeActivity.java
uk.ac.icappsoc.appsocthree.compass.CompassActivity.java
uk.ac.icappsoc.appsocthree.compass.CompassView.java
uk.ac.icappsoc.appsocthree.light.LightClockActivity.java
uk.ac.icappsoc.appsocthree.proximity.ProximityActivity.java
uk.ac.icappsoc.appsoctwo.MainActivity.java
uk.ac.icappsoc.appsoctwo.circleimage.CircleImageActivity.java
uk.ac.icappsoc.appsoctwo.circleimage.CircleImageView.java
uk.ac.icappsoc.appsoctwo.circleimage.SessionTitleActivity.java
uk.ac.icappsoc.appsoctwo.rainbowtv.RainbowTextViewActivity.java
uk.ac.icappsoc.appsoctwo.rainbowtv.RainbowTextView.java
uk.ac.icappsoc.appsoctwo.split.SplitActivity.java
uk.ac.icappsoc.appsoctwo.split.SplitGameView.java