Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android學習-android內置傳感器

android學習-android內置傳感器

編輯:關於Android編程

android 內置傳感器

光線傳感器

代碼:

package com.example.administrator.sss;

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;

/**
 * Created by Administrator on 2016/12/25 0025.
 */

public class android_sensor_sss extends Activity {
    TextView acc;
    TextView luk;
    private SensorManager sm;
    private Sensor ligthSensor;

    public void onCreate(Bundle save){
        super.onCreate(save);
        setContentView(R.layout.android_sss_sensor);
        init_sensor();
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        //獲取Sensor對象
        ligthSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);

        sm.registerListener(new MySensorListener(), ligthSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public class MySensorListener implements SensorEventListener {

        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }

        public void onSensorChanged(SensorEvent event) {
            //獲取精度
            float acc_text = event.accuracy;
            //獲取光線強度
            float lux_text = event.values[0];
            acc.setText(":"+acc_text);
            luk.setText(":"+lux_text);
        }

    }
    private void init_sensor() {
        acc = (TextView)findViewById(R.id.acc);
        luk = (TextView)findViewById(R.id.luk);

    }

}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved