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

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

发布时间:2020-09-18 06:51:58

来源:脚本之家

阅读:111

作者:yangshuolll

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

实现效果图:

66a9ba77f93ca714f1df4db47ea5421b.png

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

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">

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" />

AndroidManifest.xml

package="hk.ust.cse.comp107x.ligthsensor">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:supportsRtl="true"

android:theme="@style/AppTheme">

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值