Is it possible to Sync only newly inserted Contact in Phonebook in an android app ? Suppose I hv sync my Phonebook with an app for first time and save these Contacts. Again when I need to display those Contacts then only the newly inserted Contacts will be sync and others are only retrieve from database.
Asked
Active
Viewed 85 times
2
-
I think this link will help you and you can solve your problem : https://stackoverflow.com/a/11281244/7806873 – Nitin Patel Jun 22 '17 at 09:20
-
No actully this won't help me Because if i deleted any of contact then it won't remove that contact from the app. but yes we can update new inserted contacts. But I want accurate sync :) – Renu Jul 13 '17 at 05:46
-
well, Thanx for giving hint. – Renu Jul 13 '17 at 05:47
-
I hope these two links will guide you more : https://stackoverflow.com/questions/37925202/syncing-contacts-using-syncadapter-working and https://stackoverflow.com/questions/41988836/delete-contact-from-android-contacts – Nitin Patel Jul 13 '17 at 06:55
-
thanks it works :) – Renu Jul 13 '17 at 09:03
-
Great!!! If you don't mind add answer here or send me for future needs. – Nitin Patel Jul 13 '17 at 09:07
-
1sure, i did it with ContentObserver and service with background – Renu Jul 13 '17 at 09:10
-
give me ur email id code is more than 1200 chars n not allowed here to post :| – Renu Jul 13 '17 at 10:05
-
Did you try as answer? – Nitin Patel Jul 13 '17 at 10:06
-
no i tried as a comment – Renu Jul 13 '17 at 10:20
-
Insert as `Your Answer` section at bellow comments. – Nitin Patel Jul 13 '17 at 10:22
1 Answers
1
public class ContactListenerService extends Service {
MyContentObserver contentObserver = new MyContentObserver();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentObserver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean flag = PreferencesManager.getInstance(this).optBoolean("flag");
if(flag==false){
Toast.makeText(this, flag+" val", Toast.LENGTH_LONG).show();
}else{
PreferencesManager.getInstance(getApplicationContext()).putBoolean("flag",false);
// your method to sync contact
}
return Service.START_STICKY;
}
private class MyContentObserver extends ContentObserver {
boolean flag=false;
public MyContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
PreferencesManager.getInstance(getApplicationContext()).putBoolean("flag",true);
super.onChange(selfChange);
}
@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
}
}
public class ContactSyncScreen extends BaseFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Your other stuff
//Start the service form fragment
Intent intent=new Intent(getActivity(),ContactListenerService.class);
getActivity().startService(intent);
}
}

Renu
- 69
- 8