Sensor Based Flash On and Off
MainActivity.java
package com.example.secondapp;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Camera;
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.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SensorManager sm;
private TextView mydata;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydata = findViewById(R.id.data);
//how to get sensormanager object
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
/*List<Sensor> slist = sm.getSensorList(Sensor.TYPE_ALL);
for (Sensor s : slist) {
Log.e("error", s.getName());
Log.e("error", s.getType() + "");
}*/
Sensor s = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (s != null) {
sm.registerListener(se, s,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
SensorEventListener se = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
//Log.e("error", "sensor");
float value[] = sensorEvent.values;
//Log.e("error0", value[0] + "\t" + value[1] + "\t" + value[2]);
mydata.setText(String.valueOf(value[0]));
if (value[0] == 0.0f) {
mydata.setText("near");
flashOn();
} else {
mydata.setText("so far");
flashOff();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
Log.e("error", "accuracy");
}
};
@Override
protected void onStop() {
super.onStop();
sm.unregisterListener(se);
}
private void flashOn() {
camera = Camera.open();
Camera.Parameters p = camera.getParameters();
p.setFlashMode(Camera
.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
Log.e("error", "on");
}
private void flashOff() {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
Log.e("error", "Off");
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/data"
android:paddingBottom="20dp"
android:textSize="20sp"/>
</LinearLayout>AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.secondapp">
<uses-permission-sdk-23android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment