3

在我的一个测试设备中,我在 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}

我最初的研究表明,这是一个常见的挑眉:

  1. “该应用程序可能超出了 VM 预算并且内存不足。”
  2. “这个可以忽略。”
  3. 网络问题
  4. Activity需要很长时间才能启动(UI 线程处理过多?)
  5. 涉及HTTP的服务和广播。
  6. 并且不断...

我的问题是:

  1. “启动超时已过期”的含义是什么?系统是做什么的?(例如,一个消息来源声称它会杀死我的应用程序,但实际上我看到我的应用程序在 1-2 分钟的感知冻结后正常进行)
  2. 有没有办法在我的应用程序中收到有关此警告的通知?
4

1 回答 1

2

这是我将尝试的一般想法,但您如何处理 AsyncTask 取决于您在获得状态后正在做什么。我会做这样的事情:

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");
于 2012-07-20T16:00:31.427 回答