2

I have this class that detects when the phone is facing up and when is facing down and prints on the screen Face Up or Face Down!!

package com.example.kyriakos.androiddetectflipping;
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;
import java.util.List;

public  class Test extends Activity {

    SensorManager sensorManager;
    Sensor accelerometerSensor;
    boolean accelerometerPresent;

    TextView face;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_detect_flipping);

        face = (TextView)findViewById(R.id.face);

        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if(sensorList.size() > 0){
            accelerometerPresent = true;
            accelerometerSensor = sensorList.get(0);
        }
        else{
            accelerometerPresent = false;
            face.setText("No accelerometer present!");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(accelerometerPresent){
            sensorManager.registerListener(accelerometerListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onStop() {

        super.onStop();
        if(accelerometerPresent){
            sensorManager.unregisterListener(accelerometerListener);
        }
    }

    public SensorEventListener accelerometerListener = new SensorEventListener(){

        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {

        }

        @Override
        public void onSensorChanged(SensorEvent arg0) {

            float z_value = arg0.values[2];
            if (z_value >= 0){
                face.setText("Face UP");
            }
            else{
                face.setText("Face DOWN");
            }
        }};


}

Now i want to print the message from an another class

package com.example.kyriakos.androiddetectflipping;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class Flip extends AppCompatActivity{


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

}}

It think that onSensorChanged method is responsible for printing the message so I tried to call the method onSensorChanged inside Class Flip like this

    SensorEventListener ob = new SensorEventListener();
    ob.onSensorChanged;

But it is wrong because SensorEventListener is an anonymous inner class!!So my question is how can i call this method?I read about how to call a method from an anonymous inner class but i don't understand it very well.Sorry if my question is stupid but i am new to java,android and i am trying to learn

5
  • Where do you want to call that method? Is it inside Flip onCreate() method? Commented Mar 26, 2017 at 18:15
  • Yes i want to call it inside the Flip Class Commented Mar 26, 2017 at 18:17
  • If you want to call onSensorChanged method, you need a SensorEvent object right? I could n't find SensorEvent object anywere in your code Commented Mar 26, 2017 at 18:22
  • Something like this ?SensorEvent ob = new SensorEvent(); ob.onSensorChanged(); Commented Mar 26, 2017 at 18:25
  • 1
    No, SensorEventListener is not an anonymous class. Look! There's the name right there: SensorEventListener! Commented Mar 26, 2017 at 18:43

1 Answer 1

1

You' re not assigning the anonymous class, but its base class to ob. If you want to access it and use it's overloaded methods you'd have to do:

Test test = new Test();
SensorEventListener ob = test.accelerometerListener;

Then you can call your overriden method on it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you now i think i understand my mistake so i do it like this Test test = new Test(); SensorEventListener ob = test.accelerometerListener; ob.onSensorChanged(SensorEvent arg0); but now it says tha it can't resolve arg0 :/ Am i doing something wrong again?thank you in advance
You have to provide an argument for that. You'd have to perform it providing a SensorEvent. You can create sensEvent by creating it somehow (I don't know the ways of constructing it). Then you call the method as ob.onSensorChanged(sensEvent). It's usually provided by Android. There has been a question about it earlier stackoverflow.com/questions/8734850/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.