N
N
Nikolay Baranenko2018-04-04 22:21:06
Android
Nikolay Baranenko, 2018-04-04 22:21:06

How to make friends mSensorManager and MainActivity?

Hello.
In main_activity added 2 buttons for stop and start changes events

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private TextView textView;
    private Button button;

     private final SensorManager mSensorManager;
     private final Sensor mAccelerometer;

    public MainActivity() {
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

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


//        нажатие выход из приложения
        button =(Button) findViewById(R.id.button_exit);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);
            }
        });

//        нажатие старт измерений
        button =(Button) findViewById(R.id.button_start);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              new MainActivity();
              onResume();
            }
        });
//        нажатие стоп измерений
        button =(Button) findViewById(R.id.button_stop);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPause();
            }
        });



    }


    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

    }

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

    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

}

BUT when running in the emulator, it is forcibly stopped
. What is the problem and how to solve it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question