在android中构建自动刷新方法

Edm*_*jas 3 java android

我试图在Android应用程序中构建一个方法,该方法将导致活动每3分钟刷新一次,类似于你在twitter或facebook类型应用程序中看到的,应用程序将每隔几分钟刷新一次新闻源,但是我是无法在网上找到任何教程,让我知道如何做到这一点,任何帮助都会有很长的路要走!

Avr*_*ore 5

您将需要创建一个在后台运行的异步更新任务,并触发您的更新方法,或者只需每3分钟通过一次意图直接重新启动活动.就像是:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)