国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解 android 光線傳感器 light sensor的使用

2019-10-22 18:33:32
字體:
來源:轉載
供稿:網友

調用anroid的光線傳感器使用。

實現效果圖:

android,光線傳感器,light,sensor的使用,光線傳感器的使用

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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东港市| 阜平县| 津市市| 上虞市| 改则县| 江门市| 清徐县| 仙桃市| 襄垣县| 大连市| 常山县| 靖州| 东光县| 郸城县| 张家港市| 东城区| 贺兰县| 太白县| 龙门县| 南宁市| 阜新市| 红河县| 海伦市| 井研县| 南汇区| 临沭县| 江油市| 枣强县| 呼玛县| 兴和县| 库伦旗| 祁门县| 德化县| 林口县| 定襄县| 湖北省| 长白| 青田县| 都匀市| 山阳县| 交城县|