0
我正在嘗試在已正常工作的應用程序中創建接近傳感器。我能夠運行傳感器,但是當我這樣做時,它會禁用應用程序按鈕的OnClickListener,我真的不明白爲什麼。使用Android接近傳感器時禁用了OnClickListener
這是我MainActivity.class
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button option1 = (Button) findViewById(R.id.option1);
Button option2 = (Button) findViewById(R.id.option2);
option1.setOnClickListener(this);
option2.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent();
intent.setClass(getBaseContext(),ProximitySensor.class);
startActivity(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.option1:
// Option1
break;
case R.id.option2:
// Option1
break;
default:
break;
}
}
}
而這裏ProximitySensor.class一個好的工作
public class ProximitySensor extends Activity implements SensorEventListener{
private SensorManager sm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onStart(){
super.onStart();
Sensor proximitySensor= sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximitySensor == null){
Toast.makeText(ProximitySensor.this,"No Proximity Sensor Found! ",Toast.LENGTH_LONG).show();
}
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
super.onStop();
sm.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
if(event.values[0] == 0){
Toast.makeText(ProximitySensor.this,"You are close",Toast.LENGTH_LONG).show();
}
}
}
}
我想我可能是錯打電話的傳感器,任何人都可以給我一個手?
非常感謝!
這是有道理的......我會嘗試!非常感謝! – user3637183