在不同的UI上做背景的东西

时间:2016-01-12 01:29:01

标签: android performance background-process

我设计了一个应用程序来获取网络信息并每5秒更新一次UI。 建议在与UI线程不同的线程上执行后台进程,我这样做了......但我仍然收到错误:

  

“我/编舞:跳过3730帧!应用程序可能正在做   在其主线上做了太多工作。“

为什么? 这是我的代码

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类中,我有这个私有函数:

 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方法来处理后台工作......这是我的代码:

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。

2)我应该在MainActivity中这样调用它:new MyAsyncTask()。execute();

全部谢谢

1 个答案:

答案 0 :(得分:1)

我不知道你为什么叫它Thread backgroundThread = new Thread(),因为runOnUiThread()确实是主线程。

您应该在asynctask中尝试此操作,只需更新UI中的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);
    }
}