处理程序在 ondestroy() 之后不更新 UI
handler doesn't update UI after ondestroy()
我有一个 Handler 在 activity 中声明为全局变量。该处理程序从线程接收消息并更新 de 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();
我用线程来解决这个问题。我不使用处理程序,而是使用了广播接收器。我需要的全局变量,我将它们保存在应用程序级别,这样当在 activity.
中调用 onDestroy 时我不会丢失它们
我有一个 Handler 在 activity 中声明为全局变量。该处理程序从线程接收消息并更新 de 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();
我用线程来解决这个问题。我不使用处理程序,而是使用了广播接收器。我需要的全局变量,我将它们保存在应用程序级别,这样当在 activity.
中调用 onDestroy 时我不会丢失它们