2

In my application i want to close it after 5 seconds using Timer() function.It works when i am in MainActivity but when i go to another activity then the application do not close.Now how to run this Timer() function in background if i switch activity.What to do in this case?

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        public void run() {

            finish();

        }

    }, 5000); // Application will be closed after 5 seconds
  • 2
    Possible duplicate of [keep activity going in background when switched to another activity?](http://stackoverflow.com/questions/5608833/keep-activity-going-in-background-when-switched-to-another-activity) –  Mar 25 '16 at 04:05
  • Yes, you are right but there is no suitable answer there how to do this ... – Rafael_Brett Mar 25 '16 at 04:09
  • When you switch to another activity, the memory which is occupied by that activity is freed, and the `Timer` object is lost too. That's why it's not working. You could do it, by using application context. Write a function in your application that close itself, and call it from the Activity – Nguyen Quang Anh Mar 25 '16 at 04:23
  • Your `TimerTask` works just fine. It finishes the main activity, which isn't visible. You'll need to either add this code to your other activities (don't forget to cancel the other `Timer`s!), or implement an alarm using `AlarmManager`, which sends a broadcast to your app at a specified time, and have each activity register a `BroadcastReceiver` which closes the activity. – 323go Mar 25 '16 at 04:25
  • If i use Timer() in the another activity then it is possible that the user may be in MainActivity for sometime and when the user go to second activity then the Timer() starts from the start ... – Rafael_Brett Mar 25 '16 at 04:34

1 Answers1

3

You achieve this using broadcast receiver. in your activity which you want to finish you need to create broadcast receiver.

public class TestActivity extends Activity {

public static String intent_filter_finish = "com.test.finish";

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

           registerReceiver(finishReceiver,
                new IntentFilter(intent_filter_finish));

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(finishReceiver);
        super.onDestroy();
    }


    BroadcastReceiver finishReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            finish();

        }
    };

}

now in your second activity you need to send broadcast after 5 second e.g.

public class SecondActivity extends Activity {

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

       new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            sendBroadcast(new Intent(TestActivity.intent_filter_finish));

        }
    }, 5000);

    }

}

or other possible way is directly use postDelayed() method in your test activity e.g.

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            finish();

        }
    }, 5000);
Bhavesh Rangani
  • 1,490
  • 12
  • 23
  • #Bhavesh Rangani ... The code is error free in the TestActivity but in the SecondActivity it gives error in the line ... new Handler().postDelayed(new Runnable() { @Override public void run() { sendBroadcast(new Intent(TestActivity.intent_filter_finish)); } }, 5000); } It says 'Handler' is abstract; can not be instatiated – Rafael_Brett Mar 25 '16 at 09:48
  • A red line just below the new Handler() occurs – Rafael_Brett Mar 25 '16 at 09:54
  • When i hover mouse on new Handler() then it shows the message that " 'Handler' is abstract; can not be instatiated " ... – Rafael_Brett Mar 25 '16 at 09:55
  • 1
    make sure you have import ' android.os.Handler; ' . – Bhavesh Rangani Mar 25 '16 at 09:55
  • you need to put super.onDestroy(); in your test activity's onDestroy() { }; method.. – Bhavesh Rangani Mar 25 '16 at 10:49
  • i have edit the code. please check this now, in my case this will work perfect. – Bhavesh Rangani Mar 25 '16 at 10:51
  • i have put other possible way try that also and let me know. – Bhavesh Rangani Mar 25 '16 at 10:59
  • By using your other way it is just now closing the TestActivity in 5 seconds but when i go to SecondActivity before 5 seconds then the SecondActivity don't close after 5 seconds ... – Rafael_Brett Mar 25 '16 at 11:17
  • i have tested both of example and it worked perfect. kindly check again. – Bhavesh Rangani Mar 25 '16 at 11:19
  • I just replaced the sendBroadcast(...) by finish() in the SecondActivity and replaced finish() by sendBroadcast(...) in the TestActivity, and now it's working fine ... – Rafael_Brett Mar 25 '16 at 11:27
  • it's now closing activity in both, working fine , but now the problem is that if i set time to 10 seconds.On application start if i use the TestActivty for 8 seconds and then go to SecondActivity then it should close activity after 2 seconds but the timer starts from the beginnning and i have to wait again for 10 seconds ... – Rafael_Brett Mar 25 '16 at 11:34
  • any solution for that ?? – Rafael_Brett Mar 25 '16 at 11:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107325/discussion-between-bhavesh-rangani-and-rafael-brett). – Bhavesh Rangani Mar 25 '16 at 11:36