在不同的UI做背景的东西 [英] Doing background stuff on a different UI

查看:127
本文介绍了在不同的UI做背景的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设计了一个应用程序,获取网络信息并更新UI每5秒。
这是应做的比UI线程独立的线程后台进程,而我这样做......但我仍然得到一个错误是:

I have designed an app that gets the network information and updates the UI every 5 seconds. It is advised to do the background processes on a separate thread than the UI thread, and I did so...but I still get an error that:

I /编舞:跳过3730帧的应用程序可能会做
  它的主线程的工作太多了。

"I/Choreographer﹕ Skipped 3730 frames! The application may be doing too much work on its main thread."

这是为什么?
这里是我的code

Why is that? Here's my code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wifiTextView = (TextView)findViewById(R.id.wifi_textView);
    ipTextView = (TextView)findViewById(R.id.ip_val_textView);
    // and so on

    //Updating the UI every mInterval seconds, using a separate thread than UI thread
    Thread backgroundThread = new Thread() {
        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(mInterval);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //Log.d(DEBUG_TAG, "run(): Background thread");
                            MyNetwork network = new MyNetwork(getApplicationContext());  // passing the context
                            updateUI(network);
                        }
                    });
                }
            } catch (InterruptedException e) {
                wifiTextView.setText("Exception: Please Close and Restart the App");
            }
        }
    };
    backgroundThread.start();

}

在同一MainActivity类别,我有这个私有函数:

In the same MainActivity class, I have this private function:

 private void updateUI(MyNetwork network){
    // Handles updating the textviews in the UI
    //Log.d(DEBUG_TAG, "updateUI(DeepstreamNetwork)");

    if (network.isConnected()){
        wifiTextView.setText(R.string.wifi_is_on);
        wifiTextView.setTextColor(Color.GREEN);

        ipTextView.setText(network.getIpAddress());

    else {
        wifiTextView.setText(R.string.wifi_is_off);
        wifiTextView.setTextColor(Color.RED);

        ipTextView.setText("N/A");

    }
}

更新

所以,我更新了我的MainActivity类别有这个MyAsyncTask方法来处理后台工作......这是我的code:

So, I have updated my MainActivity class to have this MyAsyncTask method to handle background work...here's my code:

private class MyAsyncTask extends AsyncTask<Void, Void, MyNetwork> {
    @Override
    protected void onPostExecute(MyNetwork network) {
        updateUI(network);
    }

    @Override
    protected MyNetwork doInBackground(Void... params) {
        MyNetwork network = new MyNetwork(getApplicationContext());  // passing the context
        return network;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

两个问题:

1)我怎么用力它这样做后台任务每5秒。由于网络状态更改每隔几秒钟(断线等),所以我希望它分别更新UI。

1) how do I force it to do this background task every 5 seconds. Since the network status changes every few secs (disconnection etc...), so I want it to update the UI respectively.

2)我应该称呼它在MainActivity:新MyAsyncTask()执行();

2) should I call it like this in MainActivity: new MyAsyncTask().execute();

感谢所有

推荐答案

我不知道你为什么把它叫做发backgroundThread =新的Thread(),因为 runOnUiThread()是真正的主线程。

I dont know why you called it Thread backgroundThread = new Thread() because runOnUiThread() is really the main Thread.

您应该在的AsyncTask ,你只更新 UI onPostExecute试试这个()

You should try this in an asynctask where you only update the UI in onPostExecute()

编辑:

private class MyAsyncTask extends AsyncTask<Void, Void, String> {

    private MyNetwork network;

    @Override
    protected void onPreExecute() {
        this.network = new MyNetwork(getApplicationContext()); 
    }

    @Override
    protected String doInBackground(Void... params) {
        return network.getIpAddress();
    }

    @Override
    protected void onPostExecute(String ipAddress) {
        if (this.network.isConnected()){
            wifiTextView.setText(R.string.wifi_is_on);
            wifiTextView.setTextColor(Color.GREEN);
            ipTextView.setText(ipAddress);
        else {
            wifiTextView.setText(R.string.wifi_is_off);
            wifiTextView.setTextColor(Color.RED);
            ipTextView.setText("N/A");
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

这篇关于在不同的UI做背景的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆