ProgressDialog or ProgressBar slows down application running Android 4.1
Asked Answered
U

1

6

I'm developing an android application in which I use an AsyncTask to get information from a Web service using JSON, which is then loaded in a ListView. Until here everything works fine.

I used an ProgressDialog which is initiated in onPreExecute() method and dismissed in the onPostExecute() method. In the emulator running 2.3.3 android version, all this action takes up to 7/8 seconds. I've tested with a real device running 2.3.6 and the app has the same behavior.

However, when I use an emulator running 4.1 android version, this action takes 65 to 70 seconds. When I remove the ProgressDialog, the action takes 7/8 seconds in both android 2.3 and 4.1 versions. I've tested all these scenarios many times.

Why does this happen? I don't have any real device with 4.1 android version to test it so I'm wondering if is this an emulator issue? I've tried to use a normal progress bar circle, instead of the ProgressDialog, but the same thing happens, it just takes too long in the android 4.1 version.

Any ideas on which may be the reason for this strange behavior? :S

Thank you.

dmon and James McCracken here is the code from my activity, can you help me on this please?

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();
}
}
Uppercase answered 19/12, 2012 at 23:32 Comment(2)
I can't think of a reason this would be happening. Can you post logs?Purgative
Something's fishy here, showing any view shouldn't take any time at all. It's also unclear if you mean 0.875 seconds or "7 or 8" seconds. If you mean 7 - 8, then something is DEFINITELY wrong. You should post the code for your AsyncTask.Hargrove
F
5

I can confirm this same behavior in the emulator only, testing on 4.1. On my actual device (Galaxy S3 - 4.1) this does not happen. Also, it only seems to happen with indeterminate progress bars. I don't know why this happens, but maybe you will feel better knowing this happens to others. I'm performing almost the same actions as you, connecting to a webservice, parsing an xml response, then displaying the parsed response. FYI, I am using a ProgressBar for the android:empty function of a ListView.

EDIT: Ran some tests

I tested parsing the xml file and with the ProgressBar in the layout I got this:

Parse time: 94785 ms

So, I changed this

<ProgressBar
        android:id="@id/android:empty"
        style="@android:style/Widget.Holo.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_gravity="center_horizontal|center_vertical" 
        android:layout_weight="1"/>

to this

 <TextView 
        android:id="@id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Searching..."/>

and got this Parse time: 2642 ms

Note: my ProgressBar and ListView are in a LinearLayout and all parsing is done in an AsyncTask. This is all on the emulator. Really strange... My guess is that the emulator does not actually separate threads from the UI, so the AsyncTask and ProgressBar spinning are fighting for CPU cycles, whereas in an actual phone it operates as intended because it is not emulated.

Edit: So, after watching TaskManager on my WinXP, the emulator starts with 7 threads running. I launch my app, run through several AsyncTask's using AsyncTask.THREAD_POOL_EXECUTOR and the thread count never changes from 7 (in Task Manager) during this whole process.

Formulate answered 15/2, 2013 at 19:54 Comment(1)
I was just about to post about a similar problem when I saw this thread. I'm seeing the same thing in my 4.4.2 emulator. An AsyncTask in which I'm calling and receiving a response from an http post increased from 1-2 seconds to 80-90 seconds only when I add a progessBar to the view. It did not appear with BlueStacks running on Windows, so I recommend that others test BlueStacks as well if they don't have a Android device to test with.Against

© 2022 - 2024 — McMap. All rights reserved.