Reputation: 25
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 :)
Upvotes: 1
Views: 146
Reputation: 683
Thread is used to provide delay. Add the below code when your sensor stopped detected.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Start Activity here
}
},5000);
Upvotes: 2
Reputation: 105
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);
Upvotes: 2
Reputation: 60973
When you stop Sensor
, you can start Activity
after 5s by use Handler
like
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(...);
}
}, 5000);
Upvotes: 2
Reputation: 132992
I need to restart the activity automatically after 5 seconds, the sensorManager has stopped. Is it possible?
Yes it is possible using AlarmManager
When stopping SensorManager
provide PendingIntent
of Activity
to AlarmManager
with required delay to start Activity:
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager manager =(AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC,System.currentTimeMillis() + 6000, pendingIntent);
Upvotes: 3