Android AsyncTask: start new Activity in onPostExe

2019-03-21 10:36发布

public class HttpHelper extends 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
}

}

In this code i read the data from the server through AsyncTask and the result should be in the new UI.That means i want to start new Activity from onPostExecute().

3条回答
家丑人穷心不美
2楼-- · 2019-03-21 11:27

Try 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);
    getApplicationContext().startActivity(intent);
}
查看更多
Ridiculous、
3楼-- · 2019-03-21 11:28

Or you can get the context of your current activity by calling your current name, like this 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);
}
查看更多
你好瞎i
4楼-- · 2019-03-21 11:30

getApplicationContext() can not be called from AsyncTasck. Instead you can declare in your constructor a variable ctx that is getApplicationContext().

@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);
}
查看更多
登录 后发表回答