开发者

building an auto refresh method in android

开发者 https://www.devze.com 2023-04-04 12:49 出处:网络
Im attempting to build a method in an android application that will cause an activity to refresh every 3 minutes, similar to what you see in a twitter or facebook type app where the application will r

Im attempting to build a method in an android application that will cause an activity to refresh every 3 minutes, similar to what you see in a twitter or facebook type app where the application will refresh the newsfeed on its own every few minutes, however I am unable to find any tutorials on the web that give me an i开发者_如何转开发dea as to how this is done, any help would go a long way thanks!


You'll want to create an asynchronous update task that runs in the background and fires off either your update method, or just straight relaunches the activity via an intent every 3 minutes. Something like:

private class YourUpdateTask extends AsyncTask<Integer, Void, Integer> {
        /**
         * The system calls this to perform work in a worker thread and delivers
         * it the parameters given to AsyncTask.execute()
         */
        protected Integer doInBackground(Integer... millis) {
            try {
                int waited = 0;
                int duration = yourTimeHere;
                while (waited < duration) {
                    Thread.sleep(100);
                    waited += 100;
                }
            } catch (InterruptedException e) {
                // do nothing
            }

            updateState();

            return 1;
        }

        /**
         * The system calls this to perform work in the UI thread and delivers
         * the result from doInBackground()
         */
        protected void onPostExecute(Integer result) {
            refreshActivity();
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号