安卓的AsyncTask:在onPostExecute启动新活动()(Android AsyncTa

2019-07-31 11:19发布

公共类HttpHelper延伸的AsyncTask> {ArrayList的列表=新的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.That意味着我要开始新的onPostExecute活动()。

Answer 1:

试试这个,

@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);
}


Answer 2:

getApplicationContext()不能从AsyncTasck调用。 相反,你可以在你的构造函数声明一个变量CTX是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);
}


Answer 3:

或者,你可以通过调用当前的名字,像这样让你当前活动的背景下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);
}


文章来源: Android AsyncTask: start new Activity in onPostExecute()