Search code examples
androidandroid-intentandroid-activityaccelerometerautostart

Android - Auto starting an activity after some duration


I am new to Android.

I am having Accelerometer sensor data in one activity. I stopped the SensorManager after a shake has been detected. Now I need to restart the activity automatically after 5 seconds, the SensorManager has stopped. Is it possible?

or is it possible to start the current activity from the same?

Can somebody help me with this?

Thanks in Advance :)


Solution

  • Use this code when SensorManager has stopped. It will restart the Activity after 5 sec when SensorManager has stopped.

    new Handler().postDelayed(new Runnable() {
    
                    @Override
                    public void run() {
    
                        Intent intent = getIntent();
                        finish();
                        startActivity(intent);
                    }
                }, 5000);