Android 几个常用的 sensor 简单使用实例

光线传感器的使用实例:

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.TextView;

public class LightActivity extends Activity {

    private SensorManager sensorManager;
    private TextView lightLevel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light);

        lightLevel = findViewById(R.id.lightLevel);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (sensorManager != null) {
            Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
            sensorManager.registerListener(sensorEventListener, sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != sensorManager) {
            sensorManager.unregisterListener(sensorEventListener);
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float value = sensorEvent.values[0];
            lightLevel.setText(String.format("Current light level is %s lx", value));
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
}

对应的布局文件如下:




    


加速度传感器的使用实例:

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.Toast;

public class AccelerometerActivity extends Activity {

    private static final int THRESHOLD_VALUE = 15;

    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accelerometer);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (sensorManager != null) {
            Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(sensorEventListener, sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != sensorManager) {
            sensorManager.unregisterListener(sensorEventListener);
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float x_value = Math.abs(sensorEvent.values[0]);
            float y_value = Math.abs(sensorEvent.values[1]);
            float z_value = Math.abs(sensorEvent.values[2]);

            if (x_value > THRESHOLD_VALUE || y_value > THRESHOLD_VALUE
                    || z_value > THRESHOLD_VALUE) {
                Toast.makeText(AccelerometerActivity.this, "设备正在晃动...",
                        Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
}

加速度传感器实例对应的布局文件代码如下(默认生成的,实例中并没有用到):





利用加速传感器和方向传感器实现的简单的指南针的代码:

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.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class CompassActivity extends Activity {

    private SensorManager sensorManager;

    private ImageView iv_arrow;
    private ImageView iv_compass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compass);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (sensorManager != null) {
            Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
            sensorManager.registerListener(sensorEventListener, sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);

            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(sensorEventListener, sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }

        iv_arrow = findViewById(R.id.arrow_img);
        iv_compass = findViewById(R.id.compass_img);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != sensorManager) {
            sensorManager.unregisterListener(sensorEventListener);
        }
    }

    private SensorEventListener sensorEventListener = new SensorEventListener() {

        float[] accelerometerValues = new float[3];
        float[] magneticValues = new float[3];

        float lastRotateDegree;

        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                accelerometerValues = sensorEvent.values.clone();
            } else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                magneticValues = sensorEvent.values.clone();
            }
            // 使用 clone 方法,避免 accelerometerValues 和 magneticValues 指向同一个引用

            float[] R = new float[9];
            float[] values = new float[3];

            SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
            SensorManager.getOrientation(R, values);

            float rotateDegree = -(float) Math.toDegrees(values[0]);

            if (Math.abs(rotateDegree - lastRotateDegree) > 0) {
                RotateAnimation animation = new RotateAnimation(lastRotateDegree, rotateDegree,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setFillAfter(true);
                iv_compass.startAnimation(animation);
                lastRotateDegree = rotateDegree;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
}

指南针实例的布局文件的代码如下:




    

    


使用的图片如下:


Android 几个常用的 sensor 简单使用实例_第1张图片
compass
Android 几个常用的 sensor 简单使用实例_第2张图片
arrow

这两个图片都是从网上下载的,版权属于原作者

调用这几个 Activity 的代码如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private static List> functionInfo;

    private static final String KEY_TITLE = "title";
    private static final String KEY_CONTENT = "content";

    static {
        functionInfo = new ArrayList<>();
        Map tmpMap = new ArrayMap<>();
        tmpMap.put(KEY_TITLE, "Light");
        tmpMap.put(KEY_CONTENT, "Light Sensor Test");
        functionInfo.add(tmpMap);
        tmpMap = new ArrayMap<>();
        tmpMap.put(KEY_TITLE, "Accelerometer");
        tmpMap.put(KEY_CONTENT, "Accelerometer Sensor Test");
        functionInfo.add(tmpMap);
        tmpMap = new ArrayMap<>();
        tmpMap.put(KEY_TITLE, "Compass");
        tmpMap.put(KEY_CONTENT, "Compass Sensor Test");
        functionInfo.add(tmpMap);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter());
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return functionInfo.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup container) {
            if (functionInfo.size() <= 0) {
                return convertView;
            }

            ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, container,
                        false);
                holder = new ViewHolder();
                holder.tv_title = convertView.findViewById(R.id.title);
                holder.tv_content = convertView.findViewById(R.id.content);

                convertView.setTag(holder);
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.d(TAG, "current clicked position is " + position);
                        openActivity(position);
                    }
                });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_title.setText(functionInfo.get(position).get(KEY_TITLE));
            holder.tv_content.setText(functionInfo.get(position).get(KEY_CONTENT));

            return convertView;
        }
    }

    private static class ViewHolder {
        public TextView tv_title;
        public TextView tv_content;
    }

    private void openActivity(int position) {
        Intent intent = null;
        switch (position) {
            case 0:
                intent = new Intent(this, LightActivity.class);
                break;
            case 1:
                intent = new Intent(this, AccelerometerActivity.class);
                break;
            case 2:
                intent = new Intent(this, CompassActivity.class);
                break;
            default:
                break;
        }
        if (null != intent) {
            startActivity(intent);
        }
    }
}

其对应的布局文件如下:




    


其中用于布局 ListView 的每一项显示的布局文件如下:



    

    

本文参考自 《Android 第一行代码》

你可能感兴趣的:(Android 几个常用的 sensor 简单使用实例)