2012-09-22 65 views
0

下面提到的代碼在samasung galaxy s(GB)上完美工作,但我沒有在SE neo v(ics)和tipo(ics)上工作。這是一個索尼問題或ics問題。當啓動應用程序啓動並立即顯示「無光傳感器!退出」的敬酒。光傳感器問題

import android.app.Activity; 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class LightsenseActivity extends Activity { 

ProgressBar lightMeter; 
TextView textMax, textReading; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     lightMeter = (ProgressBar)findViewById(R.id.lightmeter); 
     textMax = (TextView)findViewById(R.id.max); 
     textReading = (TextView)findViewById(R.id.reading); 

     SensorManager sensorManager 
     = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
     Sensor lightSensor 
     = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 

     if (lightSensor == null){ 
     Toast.makeText(LightsenseActivity.this, 

      "No Light Sensor! quit-", 
      Toast.LENGTH_SHORT).show(); 
     }else{ 
     float max = lightSensor.getMaximumRange(); 

     lightMeter.setMax((int)max); 
     textMax.setText("Max Reading: " + String.valueOf(max)); 

     sensorManager.registerListener(lightSensorEventListener, 
      lightSensor, 
      SensorManager.SENSOR_DELAY_NORMAL); 

     } 
    } 

    SensorEventListener lightSensorEventListener 
    = new SensorEventListener(){ 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
    // TODO Auto-generated method stub 
    //if(event.sensor.getType()==Sensor.TYPE_LIGHT){ 

    float currentReading = event.values[0]; 
    { lightMeter.setProgress((int)currentReading); 
    textReading.setText("Current Reading: " + String.valueOf(currentReading)); 
    } 
    } 

    }; 
} 
+1

沒有光線傳感器? –

+0

@SherifelKhatib先生,這意味着手機上沒有光線傳感器。這怎麼可能 – dreamer1989

+0

我不知道。但你應該檢查規格:這些手機真的有一個光傳感器 –

回答

1

上gsmarena用於SE理學Ⅴ中的唯一的傳感器(http://www.gsmarena.com/sony_ericsson_xperia_neo_v-4122.php)和索尼的Xperia TIPO(http://www.gsmarena.com /sony_xperia_tipo-4718.php)是加速度計,接近度和指南針。

光傳感器絕不是所需的傳感器,因爲它通常用於調整屏幕亮度。從Android 4.0兼容性文檔:

7.3.7。光度計 設備實現可以包括光度計(即環境光傳感器)。

您的應用程序將不得不處理在沒有光傳感器的情況下,或者你應該android.hardware.sensor.light添加<uses-feature>到AndroidManifest.xml。