Reputation: 69
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.
Upvotes: 2
Views: 88
Reputation: 69
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);
}
}
Upvotes: 1