最近遇到一个棘手的问题,如下:public class TestService extends android.app.Service implements Runnable {
public TestService() {
} public void onStart(android.content.Intent intent, int startId) {
Thread t = new Thread(this);
t.start();
} public android.os.IBinder onBind(android.content.Intent intent) {
return null;
} public void run() {
long beg;
do {
beg = System.currentTimeMillis();
try {
Thread.sleep(3000L);
} catch (Exception e) {
}
System.out.println("#TestService: "+(System.currentTimeMillis()-beg));
} while (true);
}
}在程序的Activity里面调用以上的service:
startService(new Intent(this, TestService.class));但是得到的结果却很出乎我的预料:
07-22 17:32:11.496: INFO/System.out(11759): #TestService: 3001
07-22 17:32:14.496: INFO/System.out(11759): #TestService: 3001
07-22 17:32:17.509: INFO/System.out(11759): #TestService: 3002
关闭手机屏幕(并且USB线不能插在手机上)
07-22 17:32:35.266: INFO/System.out(11759): #TestService: 17760
开启手机屏幕或者USB线插在手机上
07-22 17:32:38.266: INFO/System.out(11759): #TestService: 3000
07-22 17:32:41.272: INFO/System.out(11759): #TestService: 3000
07-22 17:32:44.267: INFO/System.out(11759): #TestService: 3000
07-22 17:32:47.275: INFO/System.out(11759): #TestService: 3002
07-22 17:32:50.266: INFO/System.out(11759): #TestService: 3000
07-22 17:32:53.278: INFO/System.out(11759): #TestService: 3002
07-22 17:32:56.279: INFO/System.out(11759): #TestService: 3001
07-22 17:32:59.281: INFO/System.out(11759): #TestService: 3002
同样的情况,把Service换成Thread,也是一样的。还有更糟糕的,使用Object.wait(3000L)在这样的情况下也得到同样的结果。这个问题已经困扰我好几天了,没有任何进展。
不知哪位大侠能帮个忙,给小弟解开这个谜团,万分感谢!!!
public TestService() {
} public void onStart(android.content.Intent intent, int startId) {
Thread t = new Thread(this);
t.start();
} public android.os.IBinder onBind(android.content.Intent intent) {
return null;
} public void run() {
long beg;
do {
beg = System.currentTimeMillis();
try {
Thread.sleep(3000L);
} catch (Exception e) {
}
System.out.println("#TestService: "+(System.currentTimeMillis()-beg));
} while (true);
}
}在程序的Activity里面调用以上的service:
startService(new Intent(this, TestService.class));但是得到的结果却很出乎我的预料:
07-22 17:32:11.496: INFO/System.out(11759): #TestService: 3001
07-22 17:32:14.496: INFO/System.out(11759): #TestService: 3001
07-22 17:32:17.509: INFO/System.out(11759): #TestService: 3002
关闭手机屏幕(并且USB线不能插在手机上)
07-22 17:32:35.266: INFO/System.out(11759): #TestService: 17760
开启手机屏幕或者USB线插在手机上
07-22 17:32:38.266: INFO/System.out(11759): #TestService: 3000
07-22 17:32:41.272: INFO/System.out(11759): #TestService: 3000
07-22 17:32:44.267: INFO/System.out(11759): #TestService: 3000
07-22 17:32:47.275: INFO/System.out(11759): #TestService: 3002
07-22 17:32:50.266: INFO/System.out(11759): #TestService: 3000
07-22 17:32:53.278: INFO/System.out(11759): #TestService: 3002
07-22 17:32:56.279: INFO/System.out(11759): #TestService: 3001
07-22 17:32:59.281: INFO/System.out(11759): #TestService: 3002
同样的情况,把Service换成Thread,也是一样的。还有更糟糕的,使用Object.wait(3000L)在这样的情况下也得到同样的结果。这个问题已经困扰我好几天了,没有任何进展。
不知哪位大侠能帮个忙,给小弟解开这个谜团,万分感谢!!!
这样的线程还有什么用处?
你也可以启动硬件timer定时给中断啊
手机不会确定的,一般每家片子公司的都不一样,虽然软件上层都是android,但是下面驱动、kernel的配置会不一样的。
android提供的只是软件平台,硬件平台还要看芯片来定。支不支持睡眠,支持到什么程度,大家都不太一样
但是反过来想,系统内部的Google Talk等IM软件就可以在后台运行得很好,觉得很奇怪。
下载了代码看后,又觉得没有什么特别的地方,苦恼中
希望能够获得高手的解答