0

I'm working on an android project based on a motion sensor. For various purposes, I'm using an accelerometer, the gyroscope in my project.

Here is a scippet of onCreate() on onResume() method:

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*To setup Gravity*/
        flickManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Objects.requireNonNull(flickManager).registerListener(mSensorListener, flickManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 10f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
        /*End Here*/

        /*To setup YouTube API*/
        youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        youTubeView.initialize(Config.YOUTUBE_API_KEY, this);
        /*---------End-------------------*/
        /*Setup the textbox*/
        txtView = (TextView) findViewById(R.id.ttview);
        txtview2 = (TextView) findViewById(R.id.ttview2);
        txtview3 = (TextView) findViewById(R.id.ttview3);
        txtview4 = (TextView) findViewById(R.id.ttview4);
        final EditText seekToText = (EditText) findViewById(R.id.seek_to_text);
        /*-----------------End------------------------------*/

        /* Setup seekbar*/
        Button seekToButton = (Button) findViewById(R.id.seek_to_button);
        seekToButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int skipToSecs = Integer.valueOf(seekToText.getText().toString());
                player.seekToMillis(skipToSecs * 1000);
                Log.d("dur:"+skipToSecs * 1000,"Second");
            }
        });

 protected void onResume()
    {
        flickManager.registerListener(mSensorListener, flickManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
        //accelerometer.register();
        //gyroscope.register();
    }

My approach is to check up some conditions first before activating any motion-based work. Is it possible to set up any condition such as "If-else" here? If not what is the best practice will be to call them from outside of the method to see whether they should activate or not.

Thank you.

ferdous
  • 21
  • 4
  • yeah you can setup any condition you want, what have you tried so far? – Fred Sep 13 '20 at 15:30
  • I tried to use if-else condition on onResume() like ```if(flickManager.registerListener(mSensorListener, flickManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);) { accelerometer.register(); } ``` But it doesn't respond to anything in the emulator, the app seems doesn't start at all. – ferdous Sep 13 '20 at 15:50
  • If the app doesn't start it sounds like there's a crash somewhere. Can you check logcat for some logs? – Fred Sep 13 '20 at 16:40
  • Here it is: ``` java.lang.RuntimeException: Unable to resume activity {com.example.YouTilt/com.example.YouTilt.MainActivity}: android.util.SuperNotCalledException: Activity {com.example.apps/com.example.apps.MainActivity} did not call through to super.onResume() Caused by: android.util.SuperNotCalledException: Activity {com.example.apps/com.example.apps.MainActivity} did not call through to super.onResume() ``` This is what I'm having now. – ferdous Sep 13 '20 at 16:53
  • Ah ok, so that error simply says that you need to call `super.onResume()` in `onResume()`. From your code, you don't seem to be doing so. – Fred Sep 13 '20 at 18:20
  • Is this like calling `super.onResume();` first, then setup if-else condition? – ferdous Sep 13 '20 at 23:58
  • `protected void onResume() { super.onResume(); if(flickManager.registerListener(mSensorListener, flickManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL)) { accelerometer.register(); gyroscope.register(); } }` My logic was if i can detect any shake/vibrating on phone then the accelerometer and gyroscope will start to work. – ferdous Sep 14 '20 at 00:56

0 Answers0