
我试图在Android应用程序中构建一个方法,该方法将导致活动每3分钟刷新一次,类似于你在twitter或facebook类型应用程序中看到的,应用程序将每隔几分钟刷新一次新闻源,但是我是无法在网上找到任何教程,让我知道如何做到这一点,任何帮助都会有很长的路要走!
解决方法:
您将需要创建一个在后台运行的异步更新任务,并触发您的更新方法,或者只需每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(); } } 总结 以上是内存溢出为你收集整理的java – 在android中构建自动刷新方法全部内容,希望文章能够帮你解决java – 在android中构建自动刷新方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)