2014-11-17 3 views
-1

코드에 문제가 있습니다. 두 가지 클래스가 있습니다 : MainActivity 센서 (이 경우에는 광 센서)를 사용할 수 있는지 그리고 예인 경우 다른 클래스의 LightSensor에서 센서로 날짜를 가져 오려고하지만 결과는 항상 null입니다. 나는 내가 청취자와 함께 뭔가 잘못하고 있다고 생각하지만, 나는 무엇을 해야할지 모르겠다. 나는 몇 시간 동안 앉아 있었고 아직도 아무것도하지 않았다. 어떤 생각이 든다면, 글을 쓰고 나를 도와주세요.별도의 클래스에서 센서의 데이터를 읽는 방법

`public class MainActivity extends Activity implements EventListener { 

    SensorManager mSensorManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     TextView wyswietl = (TextView)findViewById(R.id.res); 

     mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
     LightSensor mLightSensor = new LightSensor(getBaseContext()); 

     if (mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) != null){ 
      //mLightSensor.register(); 
      String newLux = mLightSensor.getLux(); 
      wyswietl.setText("Light level: " + newLux); 
     } 
     else{ 

     } 
} 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    }` 

및 MainActivioty 클래스에 내가 그것을 생성자 인수 모른다 :

MainActivity 클래스

LightSensor mLightSensor = new LightSensor(getBaseContext()); 좋은 ...

LightSensor 클래스 :

`public class LightSensor implements SensorEventListener { 

    public SensorManager mSensorManagerx; 
    public Sensor lightManager; 
    public String lux; 
    Context context; 

    public LightSensor(Context context){ 
    //public void onCreateLight(Context context){ 
     this.context = context; 
     mSensorManagerx = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); 
     lightManager = mSensorManagerx.getDefaultSensor(Sensor.TYPE_LIGHT); 
    } 

    public void register(){ 
     mSensorManagerx.registerListener(this, lightManager, SensorManager.SENSOR_DELAY_NORMAL); 
    } 
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     // TODO Auto-generated method stub 
    } 
    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // TODO Auto-generated method stub 
     lux = Float.toString(event.values[0]); 
    } 
    public String getLux(){ 
     return lux; 
    } 

    public void unregister(){ 
     mSensorManagerx.unregisterListener(this); 
    } 

}` 
+0

당신이 시스템을'onSensorChanged'를 호출 할 수있는 기회를 제공하지 않습니다. 당신은'register'를 호출하지 않고'lux'가 설정되어 있는지 확인하지 않습니다. – njzk2

답변

0

onSensorChanged 리스너를 만든 직후에 이벤트가 호출되지 않습니다. 그것은 무언가가 sensot 값을 바꿀 것이라고 불리는 다음 호출됩니다. 그래서 당신은, 나에게로, 더 나은

wyswietl.setText("Light level: " + newLux); 
+0

이 코드 줄을 나에게 쓸 수 있습니까? Unfortunetelly 나는 너를 이해하지 못한다. – izek

+0

위가 옳은 대답이다. –

1

당신이 목적을 위해 getter을 사용해서는 안 그럼 그냥 onSensorChanged 이벤트 메서드 호출에, 당신의 활동에 SensorEventListener를 구현하는 것, 어떤 콜백을 구현, 또는해야하는 값 때문에 당신의 도착은 시간이 알려지지 않은 후에 초기화 될 것입니다.. 따라서 getLux 메서드를 호출 할 때 null이 될 수 있습니다.

수신자 패턴을 사용해야합니다. 예제 구현을 위해 코드를 약간 변경했습니다.

LightSensor :

public class LightSensor implements SensorEventListener { 

    public static interface LightSensorListener { 
     abstract void onLightSensorChanged(String lux); 
    } 

    private LightSensorListener listener; 

    private SensorManager mSensorManagerx; 
    private Sensor lightManager; 

    public LightSensor(Context context) { 
     mSensorManagerx = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 
     lightManager = mSensorManagerx.getDefaultSensor(Sensor.TYPE_LIGHT); 
    } 

    public void setListener(LightSensorListener listener) { 
     this.listener = listener; 
    } 

    public boolean register() { 
     return mSensorManagerx.registerListener(this, lightManager, SensorManager.SENSOR_DELAY_UI); 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     if (listener != null) { 
      listener.onLightSensorChanged(Float.toString(event.values[0])); 
     } 
    } 

    public void unregister() { 
     mSensorManagerx.unregisterListener(this); 
    } 
} 

활동 :

public class ActivityLightSensor extends Activity implements LightSensorListener { 

    private TextView text; 
    private LightSensor mLightSensor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     text = (TextView)findViewById(R.id.test); 

     mLightSensor = new LightSensor(getBaseContext()); 
     mLightSensor.setListener(this); 
    } 

    @Override 
    public void onLightSensorChanged(String lux){ 
     text.setText("Light level: " + lux); 
    } 

    @Override 
    protected void onResume() { 
     super.onStart(); 
     if(!mLightSensor.register()){ 
      Toast.makeText(this, "Light sensor not supported!", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onStop(); 
     mLightSensor.unregister(); 
    } 
} 
+0

감사합니다. 하지만 전화가 텍스트 나 결과를 표시하지 않기 때문에 뭔가 잘못되었습니다. 화면에 빈 필드가 표시됩니다. – izek

+0

Light 센서를 활동의 onStart 메소드에 등록 했습니까? –

+0

내 코드는 위의 코드와 똑같습니다. – izek