• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

how How to send user back to login activity after a specific idle timeout android

P

pradeep24

Guest
I want to send user back to the Login activity after idle time of 10 secs. If user clicks again on screen before 10 secs are over, time will automatically reset to new 10 sec and so on...
I want to use the same login on multiple pages. I am now using this code - but if I implement it on every activity, it doesn't work property. It gives Session time out toast before 10secs many times on different activities. Please help me with code :-(



public class SecondActivity extends Activity
{

int timeout;
Handler hl_timeout = new Handler();

Button ok;

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

timeout = 10000;
hl_timeout.postDelayed(DoOnTimeOut, timeout);

ok=(Button)findViewById(R.id.btn_sec);

ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
}

Thread DoOnTimeOut = new Thread()
{

public void run()
{
try
{
Intent intent = new Intent(SecondActivity.this,FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
DoOnTimeOut.stop();
finish();
Toast.makeText(getApplicationContext(), "Session Expired 2st page", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
};

@Override
public void onUserInteraction()

{
super.onUserInteraction();
//Remove any previous callback
try
{
hl_timeout.removeCallbacks(DoOnTimeOut);
hl_timeout.postDelayed(DoOnTimeOut, timeout);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
 
I want to send user back to the Login activity after idle time of 10 secs. If user clicks again on screen before 10 secs are over, time will automatically reset to new 10 sec and so on...
I want to use the same login on multiple pages. I am now using this code - but if I implement it on every activity, it doesn't work property. It gives Session time out toast before 10secs many times on different activities. Please help me with code :-(



public class SecondActivity extends Activity
{

int timeout;
Handler hl_timeout = new Handler();

Button ok;

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

timeout = 10000;
hl_timeout.postDelayed(DoOnTimeOut, timeout);

ok=(Button)findViewById(R.id.btn_sec);

ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
}

Thread DoOnTimeOut = new Thread()
{

public void run()
{
try
{
Intent intent = new Intent(SecondActivity.this,FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
DoOnTimeOut.stop();
finish();
Toast.makeText(getApplicationContext(), "Session Expired 2st page", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
};

@Override
public void onUserInteraction()

{
super.onUserInteraction();
//Remove any previous callback
try
{
hl_timeout.removeCallbacks(DoOnTimeOut);
hl_timeout.postDelayed(DoOnTimeOut, timeout);
}catch(Exception e)
{
e.printStackTrace();
}
}
}

I'm just learning about programming myself, but if you switch the timout to a service that listens to the onTouch handler then that might work.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones