我正在開發一個Android應用程序,其中使用AsyncTask從Web服務中使用JSON獲取信息,然後將該信息加載到ListView中。直到這裏一切正常。ProgressDialog或ProgressBar會降低運行Android 4.1的應用程序
我使用了一個在onPreExecute()方法中啓動並在onPostExecute()方法中被解散的ProgressDialog。在運行2.3.3 android版本的模擬器中,所有這些操作最多需要7/8秒。我已經使用運行2.3.6的真實設備進行了測試,並且該應用程序具有相同的行爲。
但是,當我使用運行4.1 android版本的模擬器時,此操作需要65到70秒。當我刪除ProgressDialog時,動作在Android 2.3和4.1版本中都需要7/8秒。我已經多次測試過所有這些場景。
爲什麼會發生這種情況?我沒有任何真正的4.1 Android版本的設備來測試它,所以我想知道這是一個模擬器問題?我試過使用普通的進度條圈,而不是ProgressDialog,但同樣的事情發生,它只需要很長的時間在android 4.1版本。
任何想法可能是這種奇怪行爲的原因? :S
謝謝。
dmon和James McCracken這裏是我的活動代碼,請問您能幫我嗎?
public class MainActivity extends Activity {
private TextView tvTitleList;
private ListView lvDataSearch;
private ProgressDialog dialog;
private long timeStart;
private String[] dataSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTitleList = (TextView) findViewById(R.id.tvTitleList);
lvDataSearch = (ListView) findViewById(R.id.lvDataSearch);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class ProcessUpdateList extends AsyncTask<Integer, String, String[]> {
@Override
protected void onPreExecute() {
timeStart = System.currentTimeMillis();
System.out.println("pre execute");
try {
tvTitleList.setText("working....");
dialog = ProgressDialog.show(MainActivity.this, "", "Loading. Please wait...", true);
} catch (Exception e) {
System.out.println("Error Async 1: " + e.getMessage());
}
}
@Override
protected String[] doInBackground(Integer... paramss) {
try {
System.out.println("Get Data");
dataSearch = getDataSearch();
} catch (Exception e) {
System.out.println("Error Async 2: " + e.getMessage());
}
System.out.println("Return Data "
+ ((System.currentTimeMillis() - timeStart)/1000));
return dataSearch;
}
@Override
protected void onPostExecute(String[] values) {
try {
System.out.println("Post Execute Data");
tvTitleList.setText("done in " + ((System.currentTimeMillis() - timeStart)/1000));
updateViewList();
dialog.dismiss();
} catch (Exception e) {
System.out.println("Error Async 3: " + e.getMessage());
}
}
}
public void eventUpdateList() {
ProcessUpdateList process = new ProcessUpdateList();
try {
process.execute();
} catch (Exception e) {
System.out.println("NEW ERROR 2: " + e.getMessage());
}
}
public String[] getDataSearch() {
try {
String readTwitterFeed = readJSON2("http://search.twitter.com/search.json?q=hey");
JSONObject jsonObject = new JSONObject(readTwitterFeed);
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObject);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(jsonObject);
JSONObject jsonObject2 = new JSONObject(json);
JSONObject level1 = jsonObject2.getJSONObject("nameValuePairs");
JSONObject level2 = level1.getJSONObject("results");
JSONArray level3 = level2.getJSONArray("values");
dataSearch = new String[level3.length()];
System.out.println(level3.length()+" resultados encontrados.");;
for(int i=0; i<level3.length(); i++) {
dataSearch[i] = level3.getJSONObject(i).getJSONObject("nameValuePairs").getString("text");
}
}
catch(JSONException e) {
System.out.println(e.getMessage());
}
return dataSearch;
}
public String readJSON2(String command) {
JSONObject jObj = null;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(command);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
jObj = new JSONObject(builder.toString());
} else {
System.out.println(MainActivity.class.toString() + " Failed to download file");
}
} catch (Exception e) {
return null;
}
return builder.toString();
}
public void updateViewList() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataSearch);
lvDataSearch.setAdapter(adapter);
}
public void clickBRefreshList(View v) {
eventUpdateList();
}
}
我想不出會發生這種情況的原因。你可以發佈日誌嗎? –
這裏有點可疑,顯示任何視圖都不應該花費任何時間。目前還不清楚你的意思是0.875秒還是「7或8」秒。如果你的意思是7 - 8,那麼肯定是錯的。你應該發佈你的AsyncTask的代碼。 – dmon