Well I am having a stranger problem here in my android app.
I made an alarm that starts a service, something like this:
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 {
};
So, every 30 seconds alarm manager starts the service and it accesses the webpage http://www.mysite.com.br. This code works fine in emulator but not on my device. When the phone sleeps (and the screen blackout) the webpage is not accessed and application returns an error in catch (Throwable e): “java.net.UnknownHostException: Host is unresolved” or return a socket error.
The strange thing is that if I press any button on the device and the screen lights up the application returns to work normally and the web site is accessed.
Does anyone know how to solve this problem?
Thanks for some help
Alexandre
This is normal. When device goes to sleep some functions are not available.
AlarmManagerholds a CPU wake lock, so that youronReceive()method gets executed when alarm goes off. Note that this only affects this method: if you start a service this guarantee does not extend to this service. Also, it only gives you CPU wake lock, meaning that CPU will be running, but not the screen and network.To access the Wifi from your service you need to acquire the
WifiManager.WifiLock. For 3g, screen and other functions seePowerManager.Also, running network scan every 30s all the time will drain the battery in a matter of hours – users will not like this. Reconsider using server push (with wifi lock) or sync adapter.