English 中文(简体)
我是如何接手AyncTask的?
原标题:How do I run an AsyncTask over and over?

我有一种可操作以下代码的惯用方法。 简言之,该代码检索服务器的数据,并显示其为信息传送方案的屏幕上。 它只是一次,但我希望它每3秒管理AsyncTask(试图模拟聊天)。 我很相信,这不是建立聊天系统的途径,但我只需要现在能够发挥作用的东西(作为概念的证明),而且我很重视稍后执行这一制度的正确方式。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_box);// sd

        final Functions function = new Functions();
        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        whatroom = prefs.getString("chat", "null");

        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... args) {
                return function.getInbox(args[0]);
            }

            @Override
            protected void onPostExecute(String result) {
                TextView inbox = (TextView) findViewById(R.id.inbox);
                ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
                progressBar.setVisibility(View.GONE);
                inbox.setText(result);
            }

        }.execute(whatroom);
    }

I ve tried putting a simple while statement around the asynctask but, it just force closes.

最佳回答

不能再使用<条码>AsyncTask。 你们需要创造新鲜的经历,每次都穿过你。

问题回答

Without additional information, it s difficult to give you a specific answer. However look into abstracting everything using a Loader, using a Service, etc

关于<代码>Loaders :

  • They are available to every Activity and Fragment.
  • They provide asynchronous loading of data.
  • They monitor the source of their data and deliver new results when the content changes.
  • They automatically reconnect to the last loader s cursor when being recreated after a configuration change. Thus, they don t need to re-query their data.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签