سؤال

I want to build a few conditions to kick a user out of an activity after X amount of time.

I get the general idea:

  • store the current time in milliseconds , from a calendar object in milliseconds
  • determine how long I want the user to be in the activity
  • add the how long + stored current time to determine what calendar time in milliseconds to boot close that activity
  • compare the kickout time to the present time in milliseconds

for the last step I found that the best "time listener" I could find was TimerTask, implemented as such:

protected void onPostExecute(Boolean success){

      kickout.schedule(new KickoutTask(), 5000);

}

class KickoutTask extends TimerTask {
    public void run() {
        Looper.prepare();

            replaceRootView(getLocalActivityManager().startActivity("myPreviousActivity", new
                Intent(this, myPreviousActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView());

        Looper.loop();

    }
}

This is at the final part of an AsyncTask, the counting and "time listening" actually works, but when it comes to replace the view in the run() method, the app crashes with

"Only the original thread that created a view hierarchy can touch its views."

I understand the concept but I don't understand how else to get out of this activity upon resolution of the counting in the schedule function. Also I need to "replace" the view instead of just using "finish()" for this activity because of some refreshing that needs to take place.

I would like to use another simple AsyncTask, but the schedule function requires a TimerTask

Insight Appreciated

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top