Pass parameter from an Activity to a Class

1

I have an ACTIVITY

   public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private Sensor mBrujula;
    private Button iniciar_crono;
    private TextView x_a;

    private DibujarBrujula brujula;

    //
    private InetAddress IPAddress;
    public float azimut;
    private int puertoServ = 5000;
    private DatagramSocket clientSocket;


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

        brujula=new DibujarBrujula(this);
        setContentView(brujula);


        x_a = (TextView) findViewById(R.id.azimut);

        try {

            DatagramSocket clientSocket = new DatagramSocket();
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);

            mBrujula = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
            mSensorManager.registerListener(this, mBrujula, SensorManager.SENSOR_DELAY_FASTEST);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == mAccelerometer.getType()) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            enviarDatosAcelerometro(x, y, z, azimut);
        } else if (event.sensor.getType() == mBrujula.getType()) {
            this.azimut = event.values[0];
            x_a.setText("x = " + Float.toString(event.values[0]));//para probar que se ven los valores del los sensores
        }
    }

    public void enviarDatosAcelerometro(float x, float y, float z, float azimut2) {
        String info = "x=" + x + " y=" + y + " z=" + z + " grados=" + azimut2;
        new EnviarDatos(info).execute();
    }

    @Override
    public void onClick(View view) {

    }


    private class EnviarDatos extends AsyncTask<String, Void, Void> {
        private String info;

        public EnviarDatos(String info) {
            this.info = info;
        }

        protected Void doInBackground(String... arg0) {
            try {
                byte[] bytesInfo = info.getBytes();
                DatagramPacket sendPacket = new DatagramPacket(bytesInfo, bytesInfo.length, IPAddress, puertoServ);
                clientSocket.send(sendPacket);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPreExecute() {
        }

        protected void onPostExecute(Void result) {
        }
    }

}

And I want to pass the azimuth parameter to the class DrawBruch, to be able to use the value of that sensor

public class DibujarBrujula extends View {

private float direction;
private Bitmap bitmapAguja, bitmapVelocidad;
private double gradosVel, gradosVelActual;
public Matrix matrixAguja, matrixBrujula;

public DibujarBrujula(Context context) {
    super(context);
}

public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int height = this.getHeight();
    int width = this.getWidth();
    canvas.drawColor(Color.TRANSPARENT);
    if (bitmapAguja == null)
        bitmapAguja = BitmapFactory.decodeResource(getResources(), R.drawable.aguja);
    if (bitmapVelocidad == null) {
        bitmapVelocidad = BitmapFactory.decodeResource(getResources(), R.drawable.esfera);
        bitmapVelocidad = Bitmap.createScaledBitmap(bitmapVelocidad, width, width, false);
    }
    matrixAguja = new Matrix();
    matrixAguja.setTranslate(width / 2 - bitmapAguja.getWidth() / 2, height /2 - bitmapAguja.getHeight()+600 );
    matrixAguja.postRotate(-direction , width / 2, height / 2);
    matrixBrujula = new Matrix();
    matrixBrujula.postTranslate(width / 2 - bitmapVelocidad.getWidth() / 2, height / 2 - bitmapVelocidad.getWidth() / 2);
    double difGradosVel = gradosVel - gradosVelActual;
    if (difGradosVel != 0 && Math.abs(difGradosVel) > 1) {
        if (difGradosVel > 0)
            gradosVelActual++;
        else
            gradosVelActual--;
        matrixBrujula.postRotate((float) (-gradosVelActual ), width / 2, height / 2);
    } else {
        matrixBrujula.postRotate((float) (-gradosVel ), width / 2, height / 2);
    }

    canvas.drawBitmap(bitmapAguja, matrixAguja, null);
    canvas.drawBitmap(bitmapVelocidad, matrixBrujula, null);
}

public void setDirection(float direction) {
    this.direction = direction;
    this.invalidate();
}

public void setValorFinalVel(double vel) {
    double maxValorVel = 6.0;
    gradosVel = vel * 360.0 / maxValorVel;
}

}

That is, I want the 'azimuth' value of the MainActivity to be "passed" to the 'direction' value of the DrawBruch class, to work with it

I tried to create a get in the MainActivity:

        public float getAzimut(){
        return azimut;
    }

and use it in the constructor:

    public DibujarBrujula(Context context, float direction) {
    super(context);
    this.direction=MainActivity.getAzimut();
}

But I get the following text: non-static method can not be referenced from a static context

I tried to create a get in the MainActivity:

        public float getAzimut(){
        return azimut;
    }

and use it in the constructor:

    public DibujarBrujula(Context context, float direction) {
    super(context);
    this.direction=MainActivity.getAzimut();
}

But I get the following text: non-static method can not be referenced from a static context

    
asked by Antonio 26.02.2018 в 11:17
source

1 answer

1

I see a couple of strange things.

First, I do not know why you inherit Draw from View and I do not know why you do setContentView (compass). To pass azimuth seeing how you have the code, the easiest way is to create a setAzimut method but in the DrawBruch class.

public class DibujarBrujula extends View {

    private float direction;
    private Bitmap bitmapAguja, bitmapVelocidad;
    private double gradosVel, gradosVelActual;
    public Matrix matrixAguja, matrixBrujula;

    public DibujarBrujula(Context context) {
        super(context);
    }

    public setAzimut (float aux){
        this.direccion = aux;
    }

    //No pongo el resto de código de la clase porque es el mismo

Now, from the Main when you want to fill this parameter you will only have to call the method through the compass object that you already have:

brujula.setAzimut(1.234586);  //Le pasas el valor que te haga falta

I put it like that because I see that you create your compass object at the beginning of the program. The other option would be to create it once you have the azimuth data, so you could pass the parameter through the constructor.

For this, you would have to add this parameter to the constructor of the DrawBruch class, it would be like this:

public class DibujarBrujula extends View {

    private float direction;
    private Bitmap bitmapAguja, bitmapVelocidad;
    private double gradosVel, gradosVelActual;
    public Matrix matrixAguja, matrixBrujula;

    public DibujarBrujula(Context context, float aux) {
        super(context);
        direccion = aux;
    }

and when creating it from the Main it would be like this:

brujula=new DibujarBrujula(this, valor); //Siendo valor el azimut que lo quieres pasar
    
answered by 26.02.2018 / 12:38
source