I am writing Android apps using Eclipse + SDK 8, running at Android 2.2 emulator.
I create an AsyncTask to process lengthy task in background. Below is the code:
public class taskSavingFavourite extends AsyncTask<Void, Void, Integer>
{
@Override
protected Integer doInBackground(Void... arg0)
{
List<MainListClass> mList = new ArrayList<MainListClass>();
String s = null;
int addedCount= 0;
if (mainListSubTabSelectedTab == 0) mList = buildingList;
else mList = storeList;
if (mList == null) return 0;
try
{
for (int i = 0; i < mList.size(); i ++)
{
if (mList.get(i).checked == 1)
{
s = htFavourite.get(mList.get(i).id+"-"+mList.get(i).type);
if (s == null)
{
addedCount ++;
if (!favouriteList.equals(""))
favouriteList = favouriteList + ";";
favouriteList = favouriteList +
mList.get(i).id + "-" + mList.get(i).type + ":" +
mList.get(i).name;
htFavourite.put(mList.get(i).id + "-" + mList.get(i).type,
mList.get(i).name);
mList.get(i).favourite = 1;
}
}
publishProgress();
}
if (addedCount != 0)
{
SharedPreferences settings = getSharedPreferences("MGS_PRIVATE_DATA", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("favouriteList", favouriteList);
//Commit the edits!
editor.commit();
}
}
catch (Exception e) {};
return addedCount;
}
}
The problem is: When the mList is large (1000+ items), current Activity will be force closed and back to launcher Activity. I quite confirm it is not crashed because the annoying “Unfortunately the …” dialog is not coming out.
I check the logcat and when it happens, it always have this message:
12-10 14:10:28.772: D/dalvikvm(976): threadid=9: sending two SIGSTKFLTs to threadid=2 (tid=977) to cause debuggerd dump
12-10 14:10:30.842: D/dalvikvm(976): Sent, pausing to let debuggerd run
12-10 14:10:38.903: D/dalvikvm(976): Continuing 12-10 14:10:38.903: E/dalvikvm(976): **VM aborting***
I am not sure whether this is android bug or I am missing out something.
Alright, I think I found the reason: Never use tight loop inside
AsyncTask:DoinBackground.So I just put
Thread.Sleep(1)inside the loop then everything works like a charm. Maybe this will just happen in emulator, not a real device (I just guess).