android - fetching contacts using background thread crashes the app -
android - fetching contacts using background thread crashes the app -
i using background thread(asynctask) fetch contacts phone & send server using post request works 50 100 contacts crashed if contacts more 500 or 1000, there other way processing
you should create service runs in background , pushes contacts server in batches.
you can solve issue 3 steps:
1.create class extending intentservice , in onhandleintent() method , phone call function pushes contacts server.
     public class contactpushserive extends intentservice {        @override        protected void onhandleintent(intent intent) {          pushcontacts();            }      }    2.the function pushing contacts should create batches of contacts internally , force 1 batch @ time. can take batch size 50 contacts in 1 batch.
     public static void pushcontacts() {                      int batch_size = 50;          //create getcontactsfromos() fetch os contacts         list<contact> contactslist = getcontactsfromos();          if (contactslist != null && contactslist.size() > 0) {              // batching contact  force             (int = 0; < (contactslist.size() / batch_size) + 1; i++) {                 list<contact> sublist = null;                 if ((i + 1) * batch_size > contactslist.size()) {                     sublist = contactslist.sublist(i * batch_size, contactslist.size());                 }                 else {                     sublist = contactslist.sublist(i * batch_size, (i + 1) * batch_size);                 }                  //push contacts server using                  pushcontacts(sublist);              }         }       }    3.call service code :
    //time after service should start , set 2 min     long time = system.currenttimemillis() + (2l * 60 * 1000);      //create intent invoke contactpushservice     intent intent = new intent(context, contactpushserive.class);      //start contactpushservice after 2 min using pending intent , setting alarm fire after 2 min     pendingintent contactserviceintent = pendingintent.getservice(context, -15, intent, pendingintent.flag_cancel_current);     alarmutils.setalarm(context, contactserviceintent, time);        android android-asynctask 
 
Comments
Post a Comment