【发布时间】:2011-10-18 13:02:26
【问题描述】:
好吧,我在我的 android 应用程序中遇到了一个奇怪的问题。
我发出了启动服务的警报,如下所示:
public class ExecutaAcoes extends Service implements Runnable {
protected static final String URL = "http://www.mysite.com.br"
@Override
public void onCreate() {
Log.i("Teste","Service started");
new Thread(this).start();
}
@Override
public void run() {
try {
Log.i("Teste","Thread started");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
...
} catch (Throwable e) {
Log.i("Teste","erro excepty: "+e.toString());
} finally {
};
因此,警报管理器每 30 秒启动一次服务并访问网页 www.mysite.com.br。此代码在模拟器中运行良好,但在我的设备上运行良好。当手机休眠(和屏幕黑屏)时,无法访问网页并且应用程序在 catch (Throwable e) 中返回错误:“java.net.UnknownHostException: Host is unresolved”或返回套接字错误。
奇怪的是,如果我按下设备上的任何按钮并且屏幕亮起,应用程序就会恢复正常工作并且可以访问网站。 有谁知道如何解决这个问题?
感谢您的帮助 亚历山大
【问题讨论】:
-
你能说明你是如何请求这个警报的吗? (无关:如果手机休眠,为什么必须每 30 秒更新一次?这对电池寿命不利)