详解 android 光线传感器 light sensor的使用

 更新时间:2017年06月28日 11:38:16   作者:yangshuolll  
这篇文章主要介绍了详解 android 光线传感器 light sensor的使用的相关资料,需要的朋友可以参考下

调用anroid的光线传感器使用。

实现效果图:

MainActivity.Java

package hk.ust.cse.comp107x.ligthsensor; 
 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class MainActivity extends AppCompatActivity implements SensorEventListener{ 
  private SensorManager mSensorManager; 
  private Sensor mPressure; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // Get an instance of the sensor service, and use that to get an instance of 
    // a particular sensor. 
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 
  } 
  @Override 
  public final void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // Do something here if sensor accuracy changes. 
  } 
  @Override 
  public final void onSensorChanged(SensorEvent event) { 
    float light = event.values[0]; 
    TextView v = (TextView)findViewById(R.id.textView); 
    v.setText(Float.toString(light)); 
    // Do something with this sensor data. 
  } 
  @Override 
  protected void onResume() { 
    // Register a listener for the sensor. 
    super.onResume(); 
    mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL); 
  } 
 
  @Override 
  protected void onPause() { 
    // Be sure to unregister the sensor when the activity pauses. 
    super.onPause(); 
    mSensorManager.unregisterListener(this); 
  } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  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="hk.ust.cse.comp107x.ligthsensor.MainActivity"> 
 
  <TextView 
    android:id="@+id/textView" 
    android:textSize="50dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="177dp" /> 
</RelativeLayout> 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="hk.ust.cse.comp107x.ligthsensor"> 
 
  <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
  </application> 
 
</manifest> 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Android 分享功能的实现代码

    Android 分享功能的实现代码

    这篇文章主要介绍了Android 分享功能的实现代码的相关资料,需要的朋友可以参考下
    2017-03-03
  • 解析Android开发中多点触摸的实现方法

    解析Android开发中多点触摸的实现方法

    多点触摸(MultiTouch),指的是允许计算机用户同时通过多个手指来控制图形界面的一种技术。与多点触摸技术相对应的就是单点触摸,单点触摸的设备已经有很多年了,小尺寸的有触摸式的手机,大尺寸的最常见的就是银行里的ATM机和排队查询机等等
    2013-05-05
  • Android系统对话框使用详解(最详细)

    Android系统对话框使用详解(最详细)

    这篇文章主要介绍了Android系统对话框使用详解(最详细),需要的朋友可以参考下
    2017-10-10
  • 详解如何实现一个Kotlin函数类型

    详解如何实现一个Kotlin函数类型

    这篇文章主要为大家详细介绍了如何实现一个Kotlin函数类型,文中的实现方法讲解详细,具有一定的借鉴价值,需要的小伙伴可以跟随小编一起学习一下
    2022-10-10
  • Android控件系列之EditText使用方法

    Android控件系列之EditText使用方法

    EditText是接受用户输入信息的最重要控件。通过前面课程的学习,您可能会猜到可以利用EditText.getText()获取它的文本,但真正的项目中,可能没那么简单,需要更多的限制,如文本长度限制,是否数字限制等等
    2012-11-11
  • Android编程判断SD卡是否存在及使用容量查询实现方法

    Android编程判断SD卡是否存在及使用容量查询实现方法

    这篇文章主要介绍了Android编程判断SD卡是否存在及使用容量查询实现方法,实例分析了Android针对SD卡是否存在及使用容量、全部容量等的判断技巧,非常简单实用,需要的朋友可以参考下
    2015-10-10
  • Android中Glide加载库的图片缓存配置究极指南

    Android中Glide加载库的图片缓存配置究极指南

    这篇文章主要介绍了Android中Glide加载库的图片缓存配置究极指南,Glide是一款高人气的安卓多媒体资源加载库,本文对其缓存设置和优化作了详细讲解,需要的朋友可以参考下
    2016-04-04
  • 详解Android框架MVVM分析以及使用

    详解Android框架MVVM分析以及使用

    这篇文章主要介绍了详解Android框架MVVM分析以及使用,对MVVM感兴趣的同学,一定要看一下
    2021-04-04
  • Android智能指针轻量级Light Pointer初识

    Android智能指针轻量级Light Pointer初识

    这篇文章主要为大家介绍了Android智能指针轻量级Light Pointer初识详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • c++ mk文件出错Jni调用产生java.lang.UnsatisfiedLinkError错误解决方法

    c++ mk文件出错Jni调用产生java.lang.UnsatisfiedLinkError错误解决方法

    错误产生在我把方法从c语言转为c++语言后产生的,后来检查到这种错误是因为mk文件出错,加载c文件和加载c++的文件所用的代码不一样,下面请看
    2013-11-11

最新评论