[英]Junit Testing Realm on Android. Can't create handler inside thread that has not called Looper.prepare()
[英]Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer
我试图通过在服务中创建线程来从我的php服务器接收消息,但是它不起作用。 无法进入while循环,错误消息如标题所示。 请帮忙。
//Set the schedule function and rate
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
}
private void runOnUiThread(Runnable runnable) {
// TODO Auto-generated method stub
String flag = receivermessage();
while (flag.equals("N")){
notifyUser();
}
Log.d(TAG, "Download Successful");
}
}, 0, 10000);
最好使用asynctask并在asynctask doinbackground()mtd中接收msg。由于网络服务调用可能会花费更多时间,因此您无法在Android中阻塞主线程超过5秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.