Auto refresh the activity

try this one, it works well :)

        public void onCreate(Bundle savedInstanceState)  
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.mHandler = new Handler();

        this.mHandler.postDelayed(m_Runnable,5000);


    }//onCreate

    private final Runnable m_Runnable = new Runnable()
    {
        public void run()

        {
            Toast.makeText(refresh.this,"in runnable",Toast.LENGTH_SHORT).show();

            refresh.this.mHandler.postDelayed(m_Runnable, 5000);            
        }

    };//runnable


    @Override
    protected void onPause() {
        super.onPause();
        mHandler.removeCallbacks(m_Runnable);
        finish();

    }
/*Above method needs to be there otherwise activity will be updating itself again and again even if the activity is paused i.e. back button or home button is pressed*/

You can use handler to do a loop process, like this:

Handler handler = new Handler();
Runnable refresh;

In the first call time:

refresh = new Runnable() {
    public void run() {
        // Do something
        handler.postDelayed(refresh, 5000);
    }
};
handler.post(refresh);

Since you cannot call a non-final variable inside an annonymous class, you will have to declare refresh in the containing class.

Tags:

Android