In my previous post, We have some basic understanding of Proximity sensor in android. You can go through that post if you have not seen yet.
http://androidconceptspp.blogspot.in/2015/02/proximity-sensor-in-android-part-1.html
Now, let us create the required source code for the application.
MainActivity.java
activity_main.xml
AndroidManifest.xml
http://androidconceptspp.blogspot.in/2015/02/proximity-sensor-in-android-part-1.html
In this post we will have some fun with proximity sensor. The sample application is something like below picture. When you put your finger near the proximity sensor, image will changed from icon launcher to heart shape and when you put away your finger launcher icon will be restored. Also a text will be displayed showing number of time this sensor has produced the value.
Now, let us create the required source code for the application.
MainActivity.java
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sManager;
Sensor proximity;
TextView counter;
ImageView imageView;
static int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
counter = (TextView) findViewById(R.id.sensor_counter);
counter.setText("");
proximity = sManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximity == null) {
counter.setText("Proximity Sensor Not Available");
}
imageView = (ImageView)findViewById(R.id.imageView1);
}
@Override
protected void onResume() {
super.onResume();
sManager.registerListener(this, proximity,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
count++;
counter.setText("Count: "+count);
float approx = event.values[0];
if(approx<2.0){
imageView.setImageResource(R.drawable.ic_action_favorite);
}else{
imageView.setImageResource(R.drawable.ic_launcher);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
|
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="test.example.proximitysensoactivity.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Proximity Sensor Fun"
android:textSize="25sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/sensor_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="50dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
</LinearLayout>
|
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.example.proximitysensoactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
|
No comments:
Post a Comment