我正在HTC Magic(Dev phone 2)上运行测试。我有3个背景AsyncTasks。一个从服务器下载数据,一个每分钟一次唤醒屏幕,最后一个使应用程序在给定的持续时间内保持活动状态。下载线程asynctask似乎能够正常工作,并且在下载时(〜1小时),其他两个似乎也能正常工作。但是,一旦下载完成,如果电话被拔掉,另外两个线程无法正常工作(屏幕不会唤醒,keepAlive永远不会结束)。它几乎看起来像他们完全停止,然后如果我把手机插入电脑,他们再次启动......我不知道是什么可能造成这种情况,屏幕唤醒应该是防止设备进入睡眠状态。Android设备在插入USB或未插入时表现不同
我所有的代码都可以在这里找到:https://homepage.usask.ca/~mcb394/Receive.java或者下面两个类似的东西在下载完成和拔出后似乎停止工作。任何想法都会很棒。
/**
* This class keeps the phone alive for the entire duration necessary to use all the data (think of a media player buffer)
* @author michael bullock
*/
private class KeepRunning extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(ALIVE_TIME_MS);
} catch (InterruptedException e) {
out.print(" KeepRunning Crashed!!!!!\n");
out.flush();
e.printStackTrace();
}
out.print(" KeepRunning Completed\n");
out.flush();
timeCompleted = true;
return null;
}
}
/**
* This class powers the phone's screen up every WAKEUP_EVERY ms, stays awake for 1s
* This is so the phone avoids going to sleep
* @author michael bullock
*/
private class WakeUp extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, getClass().getName());
do{
try {
Thread.sleep(WAKEUP_EVERY);
} catch (InterruptedException e) {
e.printStackTrace();
}
wl.acquire();
Log.i("", "********** Acquired Wakelock **********");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("", "********** Releasing Wakelock **********");
wl.release();
}while(!timeCompleted || !transferCompleted);
out.print(" WakeUp Completed\n");
out.flush();
return null;
}
}
更新:我在代码中放了一些时间戳记的日志语句,这里是我发现的......唤醒类应该每分钟唤醒屏幕直到下载完成。一旦网络活动停止,它将开始每隔10分钟而不是1分钟唤醒屏幕。我将KeepRunning的另一个线程分解为更小的睡眠,并显示出类似的结果。非常奇怪。 – Michael 2011-03-04 03:21:59