详解 android 光线传感器 light sensor的使用
程序员文章站
2023-11-07 15:08:46
调用anroid的光线传感器使用。
实现效果图:
mainactivity.java
package hk.ust.cse.comp107x.ligt...
调用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>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: 汉语中的一个字,令两代帝王一个笑一个哭!
下一篇: 30分钟带你了解Docker(推荐)