調用如果我有,我用它來將物體插入我的數據庫中,如果我打電話給該方法的的AsyncTask內方法的SQLiteOpenHelper類,將在DB的調用來上完成一個單獨的線程或在主UI線程?的Android凍結時使SQLiteOpenHelper從的AsyncTask
我認爲,數據庫調用會在後臺線程進行。我是否正確理解了asynctask類?我是否必須將我的SQLiteOpenHelper中的所有數據庫調用重寫爲使用asynctasks?
目前,我得到了很多的滯後自己的Android設備,並且說,一個消息上:
The application may be doing too much work on its main thread.
我打電話到與我解析一個JSON字符串響應web服務在我的doinbackground方法中,然後將項插入到數據庫中。
public class DB_Async {
public void replaceDeck(String jsonString, Delegate d){
ReplaceDeck del = new ReplaceDeck();
del.setDelegate(d);
del.addParameter("json", jsonString);
del.execute();
}
private class ReplaceDeck extends AsyncTask<String, Object, String> {
private Delegate delegate;
private Exception error;
private Hashtable<String, String> parameters = new Hashtable<String, String>();
public void setDelegate(Delegate d){
delegate = d;
}
public void addParameter(String key, String value){
parameters.put(key, value);
}
@Override
protected void onPreExecute() { //Main Thread
super.onPreExecute();
if(delegate != null)
delegate.onStart();
}
@Override
protected void onPostExecute(String result) { //Main Thread
super.onPostExecute(result);
if(delegate != null)
delegate.onResult(result, error);
}
protected String doInBackground(String... params){ //BACKGROUND THREAD
String currDate = new Date().toString();
SQLdataHelper mDataHelper = SQLdataHelper.getInstance();
String result = null;
String jsonString = parameters.get("json");
try {
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray jsonDecks = jsonObj.getJSONArray("decks");
for(int i = 0; i < jsonDecks.length(); i++){
Deck newDeck = new Deck();
int new_deck_id = mDataHelper.addDeck(newDeck); // call to SQLOpenHelper
}
} catch (JSONException e) {
e.printStackTrace();
}catch(Exception e)
{
System.out.println(e);
result = e.getMessage();
error = e;
}
return result;
}
}
我的DB只是插入新行到我的甲板柱:
public synchronized long addDeck(Deck deck){
String currDate = new Date().toString();
ContentValues values = new ContentValues();
values.put(DB_DECK_UID_COLUMN, deck.student_id);
values.put(DB_DECK_NAME_COLUMN, deck.name);
values.put(DB_DECK_DESC_COLUMN, deck.description);
values.put(DB_DECK_DIFFICULTY_COLUMN, deck.difficulty);
values.put(DB_DECK_COUNT_COLUMN, deck.cardCount);
values.put(DB_DECK_RATING_COLUMN, deck.rating);
values.put(DB_DECK_CREATED_COLUMN, currDate);
values.put(DB_DECK_UPDATED_COLUMN, currDate);
values.put(DB_DECK_WEBID_COLUMN, deck.web_id);
values.put(DB_DECK_NEED_SYNC, 0);
long deck_id = mDB.insert(DB_DECK_TABLE, null, values);
return deck_id;
}
調用這個方法,我創建了一個新的委託,並打電話給我ReplaceDeck。
DB_Async.Delegate asyncDelegate = new DB_Async.Delegate() {
@Override
public void onStart() {
// TODO Auto-generated method stub
}
@Override
public void onResult(String result, Exception e) {
Toast.makeText(mContext, "Successful Download", Toast.LENGTH_SHORT).show();
}
};
try{
DB_Async bf = new DB_Async(mContext);
bf.replaceDeck(jsonString, asyncDelegate);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
當你在異形你Traceview的應用程序,以找出真正的什麼錯誤,你學到了什麼? – CommonsWare
直到現在還沒有聽說過Traceview。謝謝你的提示!我現在檢查一下 –