4

In one of my test devices, I am receiving this dreaded warning in LogCat:

07-20 09:57:02.093: W/ActivityManager(1159): Launch timeout has expired, giving up wake lock!
07-20 09:57:02.218: W/ActivityManager(1159): Activity idle timeout for HistoryRecord{4072b5e8 com.rero.myapp/.MyActivity}

My initial research revealed that this is a common eyebrow-raiser:

  1. "The app could have exceeded the VM budget and ran out of memory."
  2. "This can be ignored."
  3. Network problem?
  4. Activity is taking to long to start (too much processing on UI thread?)
  5. Services & Broadcasts involving HTTP.
  6. And on and on...

My questions are:

  1. What are the implications of "Launch timeout has expired"? What does the system do? (e.g. one source claims it kills my application, but I actually see my application proceeding normally after a 1-2 minutes of perceived freezing)
  2. Is there a way to get notified in my application about this warning?
Community
  • 1
  • 1
Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
  • 1
    I've had this happen when I created an unintentional infinite layout pass loop. Do you have any layout or draw (OnGlobalLayout, OnPreDraw, etc.) listeners in the Activity that you are navigating *away* from? – Kevin Coppock Jul 20 '12 at 14:47
  • @kcoppock Actually I don't have any of those you mentioned, but I do attempt to `HttpURLConnection.getResponseCode()` for http://www.google.com and... it times out after **6 minutes and 36 seconds!** The Launch timeout occurs much earlier and so I am hoping to use it as a helper to handle this unusual situation. – Regex Rookie Jul 20 '12 at 15:07
  • 1
    Can you post how you're attempting the connection? I would definitely make sure it's on a separate thread (use an AsyncTask). – Kevin Coppock Jul 20 '12 at 15:10
  • @kcoppock The HTTP timeout mystery is a different subject and I just posted [a question dedicated to it](http://stackoverflow.com/q/11582390/576267). I am still interested in a way to get **notified** in my application about **Launch** timeout warnings. – Regex Rookie Jul 20 '12 at 15:31
  • 1
    I upvoted your other question. I'm not sure why that's happening, but I do think the two are probably related somehow. I'm not sure there is a way to get notified about a logged warning other than just seeing it in the logs. – Kevin Coppock Jul 20 '12 at 15:36
  • @kcoppock Thanks. I just checked and discovered that I *am* doing the HTTP on the main thread (aka UI thread). I never used AsyncTask before so a pointer to how to use it in most correct manner for HTTP would be appreciated. +1 x 3. – Regex Rookie Jul 20 '12 at 15:47

1 Answers1

2

Here's the general idea of what I would try, but how you handle the AsyncTask depends on what you're doing after you get the status. I would do something like this:

private class GetHttpStatus extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String[] params) {
        private boolean status;

        //this will be the string you pass in execute()
        String urlString = params[0];

        HttpURLConnection httpConnection;
        try {
            URL gurl = new URL(urlString);
            URLConnection connection = gurl.openConnection();
            connection.setConnectTimeout(5 * 1000);
            httpConnection = (HttpURLConnection) connection;
            int responseCode = httpConnection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK) {
                status = true;
            }
        } catch (Exception e) {
            status = false;
        } finally { 
            if(httpConnection != null) httpConnection.disconnect();
        }
        return status;
    }

    @Override
    protected Void onPostExecute(Boolean result) {
        //Here you'll do whatever you need to do once the connection
        //status has been established
        MyActivity.notifyHttpStatus(result);
    }
}

//in your Activity somewhere, you would call...

new GetHttpStatus.execute("http://www.amazon.com");
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274