作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我正在附加到 WebView 的 onPageFinished
中执行 AsyncTask
。这个方法看起来像这样:
@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
方法中更改操作栏的颜色。但是,我真的不知道如何将变量从 AsyncTask
获取到 onPageFinished
。此外,我不知道如何将颜色更改为 @color/
xml 文件中的颜色...
最佳答案
尝试这样的事情,
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]);
}
}
}
关于java - 如何在 AsyncTask 之后更改 ActionBar 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460761/
我是一名优秀的程序员,十分优秀!