I am developing an application in which I work with Android contacts and cannot move forward. In an application, the need of the application is that the contact that is being updated must send the server or the remote contact must send the server for synchronization.
I use the contact service as:
public class ContactService extends Service { private int mContactCount; Cursor cursor = null; static ContentResolver mContentResolver = null; // Content provider authority public static final String AUTHORITY = "com.android.contacts"; // Account typek public static final String ACCOUNT_TYPE = "com.example.myapp.account"; // Account public static final String ACCOUNT = "myApp"; // Instance fields Account mAccount; Bundle settingsBundle; @Override public void onCreate() { super.onCreate(); // Get contact count at start of service mContactCount = getContactCount(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // Get contact count at start of service this.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, mObserver); return Service.START_STICKY; } @Override public IBinder onBind(Intent arg0) { return null; } private int getContactCount() { try { cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if (cursor != null) { return cursor.getCount(); } else { cursor.close(); return 0; } } catch (Exception ignore) { } finally { cursor.close(); } return 0; } private ContentObserver mObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { this.onChange(selfChange, null); } @Override public void onChange(boolean selfChange, Uri uri) { new changeInContact().execute(); } }; public class changeInContact extends AsyncTask<String, Void, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... arg0) { ArrayList<Integer> arrayListContactID = new ArrayList<Integer>(); int currentCount = getContactCount(); if (currentCount > mContactCount) { // Contact Added } else if (currentCount < mContactCount) { // Delete Contact } else if (currentCount == mContactCount) { // Update Contact } mContactCount = currentCount; return ""; } @Override protected void onPostExecute(String result) { contactService = false; } // End of post } }
The problems I am facing are as follows:
A: In the above code, to get a recently updated contact, I need to check the Version of each contact from the device with my saved version of the contacts database . It took a long time for a lot of contacts.
B. To get a remote contact, I need to check that the data for the Raw id stored in my database is present on the device or not. If not, the contact is deleted. Checking whole contacts also takes too much time.
But the same as updating a contact in the what application within a few seconds, for example, from 2 to 3 seconds ...
EDIT: In the above code in the following module:
if (currentCount > mContactCount) { // Contact Added Log.d("In","Add"); } else if (currentCount < mContactCount) { // Delete Contact Log.d("In","Delete"); } else if (currentCount == mContactCount) { // Update Contact Log.d("In","Update"); }
I put a magazine. Therefore, the updater is called many times, and also when I add or remove this time too ...
I ask you to advise and suggest me what to do in order to reduce the time taken to complete the above tasks ...