2014-06-27 68 views
0

在我的應用程序中,我正在執行AsyncTask連接到WebView的onPageFinished。這種方法看起來是這樣的:如何在AsyncTask後更改ActionBar顏色?

@Override 
public void onPageFinished(WebView myWebView, String url) 
{ 
    new SendRequestAsyncTask().execute(); 

    // when a page has finished loading dismiss any progress dialog 
    if (progressDialog != null && progressDialog.isShowing()) 
    { 
     progressDialog.dismiss(); 
    } 
} 

而且SendRequestAsyncTask看起來是這樣的:

public class SendRequestAsyncTask extends AsyncTask <Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     //runs in ui thread 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("myscript.php"); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("request", "12345")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      // writing response to log 
      HttpEntity resEntity = response.getEntity(); 

      if (resEntity != null) { 

       String responseStr = EntityUtils.toString(resEntity).trim(); 
       Log.v(TAG, "Response: " + responseStr); 

       String[] parts = responseStr.split(":"); 
       parts[1] = parts[1].replace("\"", ""); 
       parts[1] = parts[1].replace("}", ""); 

       if (parts[1].equals("01")){ 
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff"))); 
       } else { 
        Log.v(TAG, "No success: " + parts[1]); 
       } 

      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     //runs in ui thread you can update the layout here 
    } 
} 

然而,這種崩潰我的應用程序。所以我的猜測是,我需要在onPageFinished方法中更改操作欄的顏色。但是,我不知道如何從AsyncTaskonPageFinished獲取變量。此外,我不知道如何將顏色從@color/ xml文件更改爲顏色...

回答

1

嘗試這樣的事情,

public class SendRequestAsyncTask extends AsyncTask <Void, String, String> { 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    //runs in ui thread 
} 

@Override 
protected String doInBackground(Void... params) { 
    // TODO Auto-generated method stub 
    String strReturn = ""; 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("myscript.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("request", "12345")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

     // writing response to log 
     HttpEntity resEntity = response.getEntity(); 

     if (resEntity != null) { 

      String responseStr = EntityUtils.toString(resEntity).trim(); 
      Log.v(TAG, "Response: " + responseStr); 

      String[] parts = responseStr.split(":"); 
      parts[1] = parts[1].replace("\"", ""); 
      parts[1] = parts[1].replace("}", ""); 

      strReturn = parts[1]; 
     } 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 

    return strReturn; 
} 

@Override 
protected void onPostExecute(String result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    //runs in ui thread you can update the layout here 

    if (result.equals("01")){ 
     actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff"))); 
    } else { 
     Log.v(TAG, "No success: " + parts[1]); 
    } 
} 
} 
1

不,您需要設置onPostExecute中的顏色。這是在UI線程上,可以安全地觸摸UI。只需在那裏打電話,你應該停止崩潰。您可能需要將一些數據傳遞給onPostExecute,以便正確執行此操作(或將其存儲在AsyncTask的類變量中)。

+0

確實有道理,我怎麼能正確地傳遞數據onPostExecute? – user3740505

+0

從doInBackground返回的值傳入onPostExecute。 –