我在一个 Activity 中有一个带有开放套接字连接的线程,我喜欢全局使用该线程,以便我可以从其他 Activity 中的线程获取数据。有点像在后台运行...
有人为我提供解决方案或示例吗?
谢谢你。
最佳答案
您正在寻找Service
或者试试这段代码
void runInBackground() {
new Thread(new Runnable() {
@Override
public void run() {
// DO your work here
// get the data
if (activity_is_not_in_background) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//uddate UI
}
});
}
runInBackground();
}
});
}
第三种方法使用异步任务-- Understanding AsyncTask
关于android - 我如何创建一个在 Android 后台运行的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145061/