Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Light Sensor

Android Light Sensor

編輯:關於Android編程

下面這段話都能看懂吧~

Here I am explaining you how to use Light Sensor of Android.
1. Implement SensorEventListener
2. Create variable for SensorManager and Sensor
3. Get Object of SensorManager using system service
4. Get Object of Light Sensor from SensorManager
5. Implement onAccuracyChanged and onSensorChanged method

 

 


[java]  package com.test; 
 
import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.util.Log; 
 
public class Test extends Activity implements SensorEventListener{ 
 
 private SensorManager mSensorManager; 
 private Sensor mLight; 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
 
   mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
         mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 
 
 } 
 @Override 
 protected void onResume() { 
   mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); 
  super.onResume(); 
 } 
 @Override 
 protected void onPause() { 
  mSensorManager.unregisterListener(this); 
  super.onPause(); 
 } 
 public void onAccuracyChanged(Sensor sensor, int accuracy) { 
   if(sensor.getType() == Sensor.TYPE_LIGHT){ 
    Log.i("Sensor Changed", "Accuracy :" + accuracy); 
   } 
   
 } 
 
 public void onSensorChanged(SensorEvent event) { 
  if( event.sensor.getType() == Sensor.TYPE_LIGHT){ 
   Log.i("Sensor Changed", "onSensor Change :" + event.values[0]); 
  } 
   
 } 

package com.test;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity implements SensorEventListener{

 private SensorManager mSensorManager;
 private Sensor mLight;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

   mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

 }
 @Override
 protected void onResume() {
   mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
  super.onResume();
 }
 @Override
 protected void onPause() {
  mSensorManager.unregisterListener(this);
  super.onPause();
 }
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
   if(sensor.getType() == Sensor.TYPE_LIGHT){
    Log.i("Sensor Changed", "Accuracy :" + accuracy);
   }
 
 }

 public void onSensorChanged(SensorEvent event) {
  if( event.sensor.getType() == Sensor.TYPE_LIGHT){
   Log.i("Sensor Changed", "onSensor Change :" + event.values[0]);
  }
 
 }
}

所有傳感器:


[java]  package androidegg.stu.sensor; 
 
import java.util.List; 
import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class AndroidEggSensorGetValueActivity extends Activity implements SensorEventListener{    //...①  
    /** Called when the activity is first created. */ 
    private SensorManager sensorManager; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        //SensorManager的接口取得  
        sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE); 
        setContentView(R.layout.main); 
    } 
     
    @Override 
    protected void onResume(){                                                //...②  
        super.onResume(); 
        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL); 
         
        //sensor1  
        for (Sensor s : sensors){ 
            sensorManager.registerListener(this,s,SensorManager.SENSOR_DELAY_NORMAL); 
        }               
    } 
     
    //  
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
        // TODO Auto-generated method stub  
         
    } 
 
    @Override 
    public void onSensorChanged(SensorEvent e) {                                   //...③  
        // TODO Auto-generated method stub  
        switch(e.sensor.getType()){ 
            //加速度的值表示  
            case Sensor.TYPE_ACCELEROMETER:{ 
                TextView x = (TextView)findViewById(R.id.x); 
                x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X])); 
                TextView y = (TextView)findViewById(R.id.y); 
                y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y])); 
                TextView z =(TextView)findViewById(R.id.z); 
                z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z])); 
                break; 
            } 
            //傾斜度的值表示  
            case Sensor.TYPE_ORIENTATION:{ 
                TextView x = (TextView)findViewById(R.id.Azimuth); 
                x.setText("Azimuth" + String.valueOf(e.values[SensorManager.DATA_X])); 
                 
                TextView y = (TextView)findViewById(R.id.Pitch); 
                y.setText("Pitch:" + String.valueOf(e.values[SensorManager.DATA_Y])); 
                 
                TextView z = (TextView)findViewById(R.id.Roll); 
                z.setText("Roll:" + String.valueOf(e.values[SensorManager.DATA_Z])); 
                break; 
            } 
            //磁力計 的值表示  
            case Sensor.TYPE_MAGNETIC_FIELD:{ 
                TextView x = (TextView)findViewById(R.id.magnetic_x); 
                x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X])); 
                TextView y = (TextView)findViewById(R.id.magnetic_y); 
                y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y])); 
                 
                TextView z= (TextView)findViewById(R.id.magnetic_z); 
                z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z])); 
                break; 
            } 
            //溫度計の値を表示する  
            case Sensor.TYPE_TEMPERATURE:{ 
                TextView x = (TextView)findViewById(R.id.degree); 
                x.setText("Degree:" + String.valueOf(e.values[SensorManager.DATA_X])); 
            } 
        } 
     
    }   
    //  
    @Override 
    protected void onStop(){ 
        super.onStop();                                                      //...④  
        sensorManager.unregisterListener(this); 
    } 

重要事項如下所示 
① 為了取得傳感器的值,需要加載SensorEventListener 
② 在onResume方法進行*********的登錄 
③ 在onSensorChanged方法中取得傳感器的值,並在畫面表示。 
     在這個方法中在onResume方法中登錄的所有種類的傳感器的值都可以取道。判別傳感器的種類,將傳感器的值用 
     TextView在畫面表示出來。 
④ 在onStop方法中接觸所有登錄的*********。 

package androidegg.stu.sensor;

import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidEggSensorGetValueActivity extends Activity implements SensorEventListener{    //...①
    /** Called when the activity is first created. */
    private SensorManager sensorManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //SensorManager的接口取得
        sensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
    }
   
    @Override
    protected void onResume(){                                                //...②
        super.onResume();
        List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
       
        //sensor1
        for (Sensor s : sensors){
            sensorManager.registerListener(this,s,SensorManager.SENSOR_DELAY_NORMAL);
        }             
    }
   
    //
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onSensorChanged(SensorEvent e) {                                   //...③
        // TODO Auto-generated method stub
        switch(e.sensor.getType()){
            //加速度的值表示
            case Sensor.TYPE_ACCELEROMETER:{
                TextView x = (TextView)findViewById(R.id.x);
                x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
                TextView y = (TextView)findViewById(R.id.y);
                y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y]));
                TextView z =(TextView)findViewById(R.id.z);
                z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z]));
                break;
            }
            //傾斜度的值表示
            case Sensor.TYPE_ORIENTATION:{
                TextView x = (TextView)findViewById(R.id.Azimuth);
                x.setText("Azimuth" + String.valueOf(e.values[SensorManager.DATA_X]));
               
                TextView y = (TextView)findViewById(R.id.Pitch);
                y.setText("Pitch:" + String.valueOf(e.values[SensorManager.DATA_Y]));
               
                TextView z = (TextView)findViewById(R.id.Roll);
                z.setText("Roll:" + String.valueOf(e.values[SensorManager.DATA_Z]));
                break;
            }
            //磁力計 的值表示
            case Sensor.TYPE_MAGNETIC_FIELD:{
                TextView x = (TextView)findViewById(R.id.magnetic_x);
                x.setText("x:" + String.valueOf(e.values[SensorManager.DATA_X]));
                TextView y = (TextView)findViewById(R.id.magnetic_y);
                y.setText("y:" + String.valueOf(e.values[SensorManager.DATA_Y]));
               
                TextView z= (TextView)findViewById(R.id.magnetic_z);
                z.setText("z:" + String.valueOf(e.values[SensorManager.DATA_Z]));
                break;
            }
            //溫度計の値を表示する
            case Sensor.TYPE_TEMPERATURE:{
                TextView x = (TextView)findViewById(R.id.degree);
                x.setText("Degree:" + String.valueOf(e.values[SensorManager.DATA_X]));
            }
        }
   
    } 
    //
    @Override
    protected void onStop(){
        super.onStop();                                                      //...④
        sensorManager.unregisterListener(this);
    }
}
重要事項如下所示
① 為了取得傳感器的值,需要加載SensorEventListener
② 在onResume方法進行*********的登錄
③ 在onSensorChanged方法中取得傳感器的值,並在畫面表示。
     在這個方法中在onResume方法中登錄的所有種類的傳感器的值都可以取道。判別傳感器的種類,將傳感器的值用
     TextView在畫面表示出來。
④ 在onStop方法中接觸所有登錄的*********。


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved