调用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 光线传感器 light sensor的使用全部内容,希望文章能够帮你解决详解 android 光线传感器 light sensor的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)