-1
我有一个Handler在活动中声明为全局变量。该处理程序接收来自线程的消息并更新de UI。处理程序不会在ondestroy()后更新UI
一切工作正常,直到应用程序发送到背景,调用onDestroy。
当我回到应用程序时,线程不断发送消息给处理程序,它会在UI上进行更改,但不会再显示这些更改。
我不想在onpause之后停止线程,因为它必须要读取我需要的一些信息,即使应用程序在后台也是如此。
我该如何得到这个工作?
这是声明为全局变量的处理程序:
Handler hRefreshData=new Handler() {
@Override
public void handleMessage(Message msg) {
edText2 = (EditText) findViewById(R.id.textarea2);
edText2.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
edText2.setTextSize(20);
edText2.setBackgroundColor(Color.rgb(255, 255, 255));
edText2.setText("");
for (int i = 0; i < tag_db_index; i++) {
if (tag_db[i].getActive() == TRUE) {
edText2.append("[" + i + "]\t");
edText2.append("ANT " + tag_db[i].tag.getAntenna() + "\t");
edText2.append("\n");
}
}
}
这是线程,开始当用户按下按钮阅读:
thrRead = new Thread() {
public void run() {
while (bRunningRead) {
// El siguiente método realiza sólo lectura (la conexión ya está abierta)
aReadTags = lectura();
processTags();
try{
semaforo.acquire();
copyaReadTags();
semaforo.release();
}
catch(InterruptedException e)
{
}
Message m = new Message();
if (bRunningRead)
hRefreshData.sendMessage(m);
}
}
};
thrRead.start();
显示您的代码。 – Piyush
您应该对服务,警报或作业执行后台任务,并在任何可见时让活动请求/侦听任务的更新。您正在使用错误的机制来完成任务 –
阅读关于Android中如何使用tondo后台任务的信息。网上有很多信息。 –