Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

android - Doing background stuff on a different 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/Choreographer﹕ Skipped 3730 frames! The application may be doing too much work on its main thread."

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();

}

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");

    }
}

UPDATE

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);
    }
}

Two issues:

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) should I call it like this in MainActivity: new MyAsyncTask().execute();

Thanks all

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

EDIT:

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...