詳解android光線傳感器lightsensor的使用

調(diào)用anroid的光線傳感器使用。

目前創(chuàng)新互聯(lián)建站已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、明溪網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

實(shí)現(xiàn)效果圖:

詳解 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> 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

新聞名稱:詳解android光線傳感器lightsensor的使用
文章網(wǎng)址:http://muchs.cn/article30/gedepo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站品牌網(wǎng)站制作、網(wǎng)站策劃、用戶體驗(yàn)、標(biāo)簽優(yōu)化、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)