我有以下课程来获取联系人并存储在我的 sqlite 数据库中。这需要大约1:45 minutes
来存储635
个联系人。
我想提高这门课程的表现。请任何人都可以提出改善这个课程的建议吗?
// Inner class for Retrieving all contacts form phone
private class ContactRetrievalService extends AsyncTask<Void, Void, Boolean>{
ProgressDialog progressDialog;
String projection[] = { Data.CONTACT_ID,
Data.RAW_CONTACT_ID,
Data.DISPLAY_NAME,
Data.HAS_PHONE_NUMBER,
Data.STARRED,
Email.DATA,
Phone.NUMBER,
StructuredPostal.STREET,
StructuredPostal.POSTCODE,
Organization.COMPANY,
Organization.TITLE};
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = ProgressDialog.show(ActivityContactRetrieval.this, "Retrieving Contacts", "Please wait...");
/* if (progressDialog == null) {
progressDialog = createProgressDialog(ActivityContactRetrieval.this);
progressDialog.show();
} else {
progressDialog.show();
}*/
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
datasource = new ContactDataSource(ActivityContactRetrieval.this);
datasource.open();
Cursor c = getContentResolver().query(
Data.CONTENT_URI,
projection,
"(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?) ",
new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE },
Data.CONTACT_ID);
while (c.moveToNext()) {
for(int i = 0; i<c.getColumnCount();i++){
Log.e("STORED " ,c.getColumnName(i) +":"+ c.getString(i));
}
Log.e("STORED END:", "-----------------------------------------------------------------------");
long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID));
final String contact_id = String.valueOf(id);
String nickName = c.getString(c.getColumnIndex(Data.DISPLAY_NAME));
String raw_contact_id = c.getString(c.getColumnIndex(Data.RAW_CONTACT_ID));
int starred = c.getInt(c.getColumnIndex(Data.STARRED));
String firstName="",lastName="";
if(nickName.contains(" ")){
firstName = nickName.substring(0, nickName.indexOf(" "));
lastName = nickName.substring(nickName.indexOf(" "),nickName.length());
}
int has_phone = c.getInt(c.getColumnIndex(Data.HAS_PHONE_NUMBER));
//To store conatct detail in Contact table
if( has_phone >= 0 && !isAlreadyExists(MySQLiteHelper.TABLE_NAME_CONTACT, MySQLiteHelper.COLUMN_CNT_CONTACT_ID, contact_id)){
Contact contactData = new Contact();
contactData.setContact_id(contact_id);
try{
if( firstName!=null || !firstName.equalsIgnoreCase("") ){//|| !firstName.equalsIgnoreCase(number)
contactData.setFirstName(firstName);
}
if( lastName!=null || !lastName.equalsIgnoreCase("") ){//|| lastName.equalsIgnoreCase(number)
contactData.setLastName(lastName);
}
}catch(Exception e){
Log.e(CLASS_NAME,e.toString());
}
contactData.setNickName(nickName);
contactData.setIsFavorite(starred);
HashMap<String, Contact> map = new HashMap<String, Contact>();
map.put("data", contactData);
long newId = datasource.createContact(map);
Log.e("insert Contact id :", String.valueOf(newId));
}
// Create Inner Thread Class
Thread phoneThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// To store phone numbers in Phone table
Cursor phone = getContentResolver().query(Phone.CONTENT_URI,new String[]{Phone._ID,Phone.NUMBER,Phone.CONTACT_ID}, Phone.CONTACT_ID + " =?", new String[]{contact_id}, null);
String number="";
while(phone.moveToNext()){
Log.e("PHONE NUMBER :",phone.getString(phone.getColumnIndex(Phone.NUMBER)));
number = phone.getString(phone.getColumnIndex(Phone.NUMBER));
if(!isAlreadyExists(MySQLiteHelper.TABLE_NAME_PHONE, MySQLiteHelper.COLUMN_PHN_NUMBER, number)){
try{
if( number!=null || !number.equalsIgnoreCase("") ){
Phones phoneData = new Phones();
phoneData.setContact_id(contact_id);
phoneData.setNumber(number);
HashMap<String,Phones> map = new HashMap<String, Phones>();
map.put("data", phoneData);
long newId = datasource.createPhone(map);
Log.e("insert phone id :", String.valueOf(newId));
}
}catch(Exception e){
Log.e(CLASS_NAME,e.toString());
}
}// else end of phone
}
phone.close();
Log.e("THREAD:","Started");
}
});
phoneThread.start();
// Create Inner Thread Class
Thread emailThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//To store email id in Email table
Cursor email = getContentResolver().query(Email.CONTENT_URI,new String[]{Email._ID,Email.DATA,Email.CONTACT_ID}, Email.CONTACT_ID + " =?", new String[]{contact_id}, null);
String emailId="";
while(email.moveToNext()){
emailId = email.getString(email.getColumnIndex(Email.DATA));
if(isAlreadyExists(MySQLiteHelper.TABLE_NAME_EMAIL, MySQLiteHelper.COLUMN_EML_EMAIL_ID, emailId)){
Log.e("DUPLICATE:","Is here");
}else{
try{
if( emailId!=null || !emailId.equalsIgnoreCase("") ){//|| !emailId.equalsIgnoreCase(number)
Emails emailData = new Emails();
emailData.setContact_id(contact_id);
emailData.setEmailId(emailId);
HashMap<String,Emails> map = new HashMap<String, Emails>();
map.put("data", emailData);
long newId = datasource.createEmail(map);
Log.e("insert email id :", String.valueOf(newId));
}
}catch(Exception e){
Log.e(CLASS_NAME,e.toString());
}
}// else end of email
}
email.close();
}
});
emailThread.start();
// Create Inner Thread Class
Thread addressThread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//To store address in address table
Cursor address = getContentResolver().query(StructuredPostal.CONTENT_URI,new String[]{StructuredPostal._ID,StructuredPostal.STREET,StructuredPostal.CITY,StructuredPostal.POSTCODE}, StructuredPostal.CONTACT_ID + " =?", new String[]{contact_id}, null);
String street="",city="",zipCode="";
while(address.moveToNext()){
street = address.getString(address.getColumnIndex(StructuredPostal.STREET));
city = address.getString(address.getColumnIndex(StructuredPostal.CITY));
zipCode = address.getString(address.getColumnIndex(StructuredPostal.POSTCODE));
if(isAlreadyExists(MySQLiteHelper.TABLE_NAME_ADDRESS, MySQLiteHelper.COLUMN_ADR_CONTACT_ID, contact_id)){
Log.e("DUPLICATE:","Is here");
}else{
try{
if( street!=null || !street.equalsIgnoreCase("")){// || !emailId.equalsIgnoreCase(number)
Address addressData = new Address();
addressData.setContact_id(contact_id);
addressData.setCity(city);
addressData.setStreet(street);
addressData.setZipCode(zipCode);
HashMap<String,Address> map = new HashMap<String, Address>();
map.put("data", addressData);
long newId = datasource.createAddress(map);
Log.e("insert email id :", String.valueOf(newId));
}
}catch(Exception e){
Log.e(CLASS_NAME,e.toString());
}
}// else end of email
}
address.close();
}
});
addressThread.start();
}
datasource.close();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Contact_saved", true);
editor.commit();
return true;
}// end of doInBackground method
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (progressDialog!=null) {
progressDialog.dismiss();
// Go to home screen
Intent i = new Intent(ActivityContactRetrieval.this, ActivityHomeScreen.class);
startActivity(i);
// And finish this activity
finish();
}
}
}
/*// Custom ProgressDialog
public static ProgressDialog createProgressDialog(Context mContext) {
ProgressDialog dialog = new ProgressDialog(mContext);
try {
dialog.show();
} catch (BadTokenException e) {
}
dialog.setCancelable(false);
dialog.setContentView(R.layout.progress_dialog);
// dialog.setMessage(Message);
return dialog;
}*/
public boolean isAlreadyExists(String table,String column,String key) throws SQLException {
int count = -1;
Cursor c = null;
MySQLiteHelper dbHelper = new MySQLiteHelper(getApplicationContext());
SQLiteDatabase database = dbHelper.getWritableDatabase();
database = dbHelper.getWritableDatabase();
try {
String query = "SELECT COUNT(*) FROM " + table + " WHERE " + column + " = ?" ;
c = database.rawQuery(query, new String[] {key});
if (c.moveToFirst()) {
count = c.getInt(0);
}
return count > 0;
}
finally {
if (c != null) {
c.close();
database.close();
}
}
答案 0 :(得分:1)
在方法isAlreadyExists
中进行以下更改:
select count
使用select * from table limit 1
答案 1 :(得分:1)
我发现了一些问题:
1.忘记所有线程。所有工作都已在AsyncTask中完成。拥有所有这些线程会增加大量的开销,特别是因为你可以同时运行几十个线程。
2.-我注意到你有这样的代码:
Address addressData = new Address();
addressData.setContact_id(contact_id);
addressData.setCity(city);
addressData.setStreet(street);
addressData.setZipCode(zipCode);
HashMap<String,Address> map = new HashMap<String, Address>();
map.put("data", addressData);
long newId = datasource.createAddress(map);
我猜最后一行在数据库中创建了一条记录。如果直接创建查询会更容易,因为创建许多对象并让它们被GC收集也会导致开销。