简体   繁体   中英

OnClickListener disabled when using Android proximity sensor

I am trying to create a proximity sensor in an application that is already working fine. I am able to run ok the sensor, but when I do it, it disables the OnClickListener of the buttons of the application and I really don't understand why.

This is my 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;
        }

    }
}

And here the ProximitySensor.class that is working OK

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();
            }
        }
    }

}

I think that I might be calling wrong the sensor, could anyone give me a hand?

Many thanks!

You need to register onClick listeners in ProximitySensor activity just like you did on your main activity.

You are currently starting a new activity, setting its layout the same as your main activity. But when you do that, the onClickListeners you set up on your main activity no longer works, because it is a new activity and a new layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM