Why my AsyncTask is not executing during application start?

82 Views Asked by At

I need to insert all phone book contacts data in my local application sqlite database on Application startup. For this i have implemented a class named LoadContacts.java which must extends Application class. Also, i have multiple classes that must be extending Application class for which i am sub classing these classes from one another.

These multiple classes are GenerateFacebookSignature extends Application, VolleyAppController extends GenerateFacebookSignature, SessionManagement extends VolleyAppController & LoadContacts extends SessionManagement. This is my class hierarchy.

The Async Task written inside LoadContacts.java is not executing because when i have checked attaching debugger in DatabaseHelper class it is returning null cursor.

This is the code for my LoadContacts.java.

public class LoadContacts extends SessionManagement {
    Context context;
    Uri contactsUri, contactsDataUri;
    Cursor contactsCursor, contactsDataCursor, birthDayCursor;
    String contactId;
    GetAllPhoneBookContacts getAllPhoneBookContacts;
    DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());

    public LoadContacts(Context context) {
        super(context);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        getAllPhoneBookContacts = new GetAllPhoneBookContacts();
        getAllPhoneBookContacts.execute();
    }

    public class GetAllPhoneBookContacts extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {
            contactsUri = ContactsContract.Contacts.CONTENT_URI;
            contactsCursor = getContentResolver().query(contactsUri, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
            if (contactsCursor != null && contactsCursor.getCount() > 0) {
                if (contactsCursor.moveToFirst()) {
                    do {
                        contactId = contactsCursor.getString(contactsCursor.getColumnIndex("_ID"));

                        contactsDataUri = ContactsContract.Data.CONTENT_URI;
                        contactsDataCursor = getContentResolver().query(contactsDataUri, null, ContactsContract.Data.CONTACT_ID + "=" + contactId, null, null);

                        String displayName = "";
                        String birthday = "";

                        if (contactsDataCursor.moveToFirst()) {
                            displayName = contactsDataCursor.getString(contactsDataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                            birthDayCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Event.DATA }, ContactsContract.Data.CONTACT_ID + "=" + contactId + " AND " + ContactsContract.Data.MIMETYPE + "= '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY, null, ContactsContract.Data.DISPLAY_NAME);
                            if (birthDayCursor != null) {
                                if (birthDayCursor.getCount() > 0) {
                                    while (birthDayCursor.moveToNext()) {
                                        birthday = birthDayCursor.getString(0);
                                    }
                                }
                            }
                            do {
                            }
                            while (contactsDataCursor.moveToNext());

                                databaseHelper.insertIntoContacts(contactId, displayName, birthday);
                        }
                    }
                    while (contactsCursor.moveToNext());
                }
            }
            return null;
        }
    }
} 

Please check that what i am doing wrong or why the contacts are not inserting in my local sqlite database. If there is a need of any other code i am ready to post it. Thanks for any help or advice.

1

There are 1 best solutions below

6
On

if SessionManagementyou have define in your Manifest file as an application class. Then you must start asyncTask in sessionManagement onCreate Method.