Android AsyncTask:在onPostExecute()中启动新的Activity

时间:2012-09-08 17:27:44

标签: android android-intent

公共类HttpHelper扩展了AsyncTask> {     ArrayList list = new ArrayList();

@Override
protected ArrayList<String> doInBackground(String... urls) {
    // TODO Auto-generated method stub

    String result="";
    for(String url:urls)
    {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try
        {
            HttpResponse response = client.execute(request);
            InputStream in = response.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while((line = br.readLine())!=null)
            {
                sb.append(line+"\n");

            }
            in.close();
            result = sb.toString();
            String pageSource = new String(result);
            int startindex = pageSource.indexOf("pdf_doc/");
             String str="";
            String []temp;
             while(startindex !=-1)
             {
                 int endindex = pageSource.indexOf(".pdf",startindex);
                 str = pageSource.substring(startindex+8, endindex);
                 String delimiter = "%20";
                 String value="";
                 temp = str.split(delimiter)  ;
                 for(int i=0;i<temp.length;i++)
                 {
                     value= value+temp[i]+" ";

                 }

                 list.add(value);
                 startindex = pageSource.indexOf("pdf_doc/",endindex);
            }
        }


        catch(Exception ex)
        {
            Log.e("Error in HTML Reading",ex.getMessage());

        }

    }

    return list;

}

@Override
protected void onPostExecute(ArrayList<String> result) {
    // TODO Auto-generated method stub

       // Here  i want to start new UI that use the result of AsyncTask
}

}

在这段代码中,我通过AsyncTask从服务器读取数据,结果应该在新UI中。这意味着我想从onPostExecute()开始新的Activity。

3 个答案:

答案 0 :(得分:19)

试试这个,

@Override
protected void onPostExecute(Void result)
{
    super.onPostExecute(result);
    Intent intent = new Intent(MyAsyncTaskActivity.this, NextActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplicationContext().startActivity(intent);
}

答案 1 :(得分:0)

无法从AsyncTasck调用

getApplicationContext()。相反,您可以在构造函数中声明一个getApplicationContext()的变量ctx。

@Override
protected void onPostExecute(Void result) 
{
    super.onPostExecute(result);
    Intent intent = new Intent(ctx.this, NextActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    ctx.startActivity(intent);
}

答案 2 :(得分:0)

或者您可以通过调用当前名称来获取当前活动的背景信息,例如MyAsyncTaskActivity.this

@Override
protected void onPostExecute(Void result)
{
    super.onPostExecute(result);
    Intent intent = new Intent(MyAsyncTaskActivity.this, NextActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MyAsyncTaskActivity.this.startActivity(intent);
}