详解android光线传感器lightsensor的使用

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

目前创新互联建站已为1000+的企业提供了网站建设、域名、虚拟主机成都网站托管、企业网站设计、明溪网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

实现效果图:

详解 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光线传感器lightsensor的使用
文章网址:https://www.cdcxhl.com/article30/gedepo.html

成都网站建设公司_创新互联,为您提供外贸建站品牌网站制作网站策划用户体验标签优化响应式网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设