Android:从线程调用方法

时间:2016-03-21 10:52:21

标签: java android methods

我在public ActionResult Edit(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } RuleAction ruleAction = UnitOfWork.RuleActionRepository.FirstOrDefault(x => x.ID == id); if (ruleAction == null) { return HttpNotFound(); } var model = new RuleActionModel(ruleAction); return View(model); } 中有一个线程处理程序,它调用名为MainActivity的方法。

处理程序/线程声明和方法都在UpdateGUI

之内

这是处理程序声明:

MainActivity

Handler handlerData = new Handler(); private Runnable runnableCode2 = new Runnable() { @Override public void run() { Log.d("Handlers","GET TOTAL RX BYTES: "+Long.toString(res) ); //Some code here that doesn't matter/ UpdateGUI(); } handlerData.postDelayed(runnableCode2, 1*6000); } }; 如下:

UpdateGUI

从记录器中我可以看到没有从线程调用public void UpdateGUI(){ Log.d("Updater", "STARTING UPDATE"); //Code that doesn't matter here} }。你能解释为什么会发生这种情况以及如何解决这个问题吗?

只是澄清一下。线程正在运行,但由于某种原因,它没有调用UpdateGUI()。

2 个答案:

答案 0 :(得分:0)

您需要至少运行一次处理程序,然后才会从处理程序runnable方法连续调用它。

所以请在代码中调用handler.post(runnableCode2);一次,然后重复

handlerData.postDelayed(runnableCode2, 1*6000);

希望这能解决您的问题。

答案 1 :(得分:0)

我看不到你开始Runnable

Handler handlerData = new Handler();

private Runnable runnableCode2 = new Runnable() {
@Override
public void run() {

 Log.d("Handlers","GET TOTAL RX BYTES:   "+Long.toString(res) );
    //Some code here that doesn't matter/
    UpdateGUI();
}
    handlerData.postDelayed(runnableCode2, 1*6000);
}
};

// This part is missing
handlerData.postDelayed(runnableCode2, 1000);