如何调用run()无限次刷新屏幕信息,直到用户关闭应用程序

时间:2016-08-06 12:41:18

标签: java android multithreading

所以我正在开发一款监控不同设备统计数据的应用。基本上我希望应用程序每秒刷新一次统计信息并将其显示给用户。例如一个简单的电池监视器

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

public class CreateWordRTLTable {

 public static void main(String[] args) throws Exception {

  XWPFDocument doc= new XWPFDocument();

  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("Paragraph 1 LTR");

  paragraph = doc.createParagraph();

  //create table:
  XWPFTable table = doc.createTable();
  //set the table itself to bidi visual
  if (table.getCTTbl().getTblPr() == null) {
   table.getCTTbl().addNewTblPr().addNewBidiVisual().setVal(STOnOff.ON);
  } else {
   table.getCTTbl().getTblPr().addNewBidiVisual().setVal(STOnOff.ON);
  }
  //create first row
  XWPFTableRow tableRow = table.getRow(0);
  tableRow.getCell(0).setText("first cell");
  tableRow.addNewTableCell().setText("السلام عليكم");
  tableRow.addNewTableCell().setText("third cell");
  //Each cell contains at least a paragraph. Those can be set to support bidi.
  for (int col = 0 ; col < 3; col++) {
   paragraph = tableRow.getCell(col).getParagraphs().get(0);
   CTP ctp = paragraph.getCTP();
   CTPPr ctppr = ctp.getPPr();
   if (ctppr == null) ctppr = ctp.addNewPPr();
   ctppr.addNewBidi().setVal(STOnOff.ON);
  }

  //create CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //values are in unit twentieths of a point (1/1440 of an inch)
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  for (int col = 1 ; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440));
  }

  //create and set column widths for all columns in all rows
  //most examples don't set the type of the CTTblWidth but this
  //is necessary for working in all office versions
  //values are in unit twentieths of a point (1/1440 of an inch)
  for (int col = 0; col < 3; col++) {
   CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
   tblWidth.setW(BigInteger.valueOf(2*1440));
   tblWidth.setType(STTblWidth.DXA);
   for (int row = 0; row < 1; row++) {
    CTTcPr cttcpr = table.getRow(row).getCell(col).getCTTc().getTcPr();
    if (cttcpr != null) {
     cttcpr.setTcW(tblWidth);
    } else {
     cttcpr = CTTcPr.Factory.newInstance();
     cttcpr.setTcW(tblWidth);
     table.getRow(row).getCell(col).getCTTc().setTcPr(cttcpr);
    }
   }
  }

  paragraph = doc.createParagraph();

  doc.write(new FileOutputStream("WordDocument.docx"));

 }
}

此代码工作正常,但如果我添加实际刷新

public class MainActivity extends AppCompatActivity implements Runnable{

public void run(){
   fillField(R.id.fieldVoltage, battery.getVolt());
}

@Override
protected void onResume(){
    super.onResume();
    run();
}

我可以看到logcat刷新,但屏幕为空,包括所有按钮和文本。我认为这是因为onResume函数实际上并没有结束。

我应该使用什么逻辑来实现刷新?

1 个答案:

答案 0 :(得分:1)

确实是因为onResume()永远不会回来。您的问题有两个原因:

  • while(true)
  • Thread.sleep(1000)

你们两个都不应该随时做。在你的情况下,这些阻止你的主线程。

而是使用主线程Handler来安排Runnable延迟。

public class MainActivity extends AppCompatActivity implements Runnable {
    private final Handler handler = new Handler();

    @Override
    protected void onResume(){
        super.onResume();
        run();
    }

    @Override
    protected void onPause(){
        handler.removeCallbacks(this);
        super.onPause();
    }

    public void run(){
        fillField(R.id.fieldVoltage, battery.getVolt());
        handler.postDelayed(this, 1000);
    }

    [....]
}