内容观察员的onChange方法1改变光标之后叫了两声观察员、光标、叫了、两声

2023-09-06 01:50:23 作者:空牢

我有一个应用程序中,我希望到Android联系人列表中的详细信息发送到远程服务器,从而使用户可以看到他的接触网上。 要做到这一点,我想通知发在手机上的联系人列表中的任何变化的远程服务器。

我已经建立了一个ContentObserver对ContactsContract.Contacts.CONTENT_URI从被启动时,手机被引导的服务。

我有一些quiestions,第2是偶然的,第三个是我的大问题。

1:有一次,我已经设置了登记在我的光标一个ContentObserver这是否只是观察员在服务中存在的服务,?我的意思是,如果该服务被杀死了,请问contentObserver继续观察?

2:我怀疑,答案是否定的,但我会问反正。反正知道哪些触点被更新时触发我contentObserver的OnChange方法?目前我有编制的手机上的所有德的联系人列表,并把他们关到我的远程服务器上,这将是容易得多,只是发送联系人的详细信息进行更新。

3:这是我的主要问题,当我进行了更改我的联系人列表中的onChange方法快速连续被解雇两次。 1变化,2调用。反正是有管理这个的?

 公共类的ContactService延伸服务{

    JSONArray contactList;

    @覆盖
    公众的IBinder onBind(意向为arg0){
        返回null;
    }

    @覆盖
    公共无效的onCreate(){
        Log.i(C2DM,内容观察家初始化);
        super.onCreate();



        //调用日志内容提供商观察员

        MyContentContactsObserver contactsObserver =新MyContentContactsObserver();
        。ContactService.this.getContentResolver()registerContentObserver(ContactsContract.Contacts.CONTENT_URI,真实,contactsObserver);

    }

    私有类MyContentContactsObserver扩展ContentObserver {

        公共MyContentContactsObserver(){
            超(空);
        }

        @覆盖
        公共无效的onChange(布尔selfChange){
            super.onChange(selfChange);
            Log.i(日志,检测到的变化中的联系人+ selfChange);
        }
    }
}
 

结果2快线在我的logcat:

 检测到变化中的联系人:假的
 检测到的变化中的联系人:假的
 

解决方案

我做了类似的实现,并设置了 ContentObserver 在日历的事件 URI 。我遇到的所有问题,你所面对现在。所以,我的解决方案/建议您的问题放在这里......

  1)当我已经设置了登记在我的光标一个ContentObserver服务,
这是否只是观察员对服务中存在吗?我的意思是,如果该服务被杀害,
请问contentObserver继续观察?
 
怎样修改自己的鼠标光标

没有,也不会超过URI观察内容的内容。但有解决这个。您可以创建一个始终运行的服务。这项服务将被重新创建尽快由于一些内存问题或其他意外解雇辞退。它会一直继续运行,除非它被明确驳回。要在服务类中创建这种服务覆盖 OnStartCommand(意向意图,诠释标志,最终诠释startId)并返回 START_STICKY 。看一看下面的code。

 公众诠释onStartCommand(意向意图,诠释标志,最终诠释startId){

    串的authenticationkey =((MainApplication)getApplicationContext())getDbConnector()getUserAuthDefault()。;
    //如果用户还没有注册,停止了此服务,它可以被称为从启动reciever。
    如果(的authenticationkey == NULL){
        stopSelf();
    }

    //重新启动,如果我们会被杀掉。
    返回START_STICKY;
}
 

  2:我怀疑,答案是否定的,但我会问反正。反正的知道哪些
接触被更新时触发我contentObserver的OnChange方法?
目前我有编制的手机上的所有德的联系人列表,并关闭它们发送到
我的远程服务器上,它会这么容易只是发送联系人的详细信息被更新。
 

您又猜对。:)。答案是第 有没有办法知道具体是哪个联系人已更新。 ContentObserver只是提供了一个事件,让你知道,有些已被更改为指向的URI数据。但我认为你正在处理的情况低效,因为你是编译所有的联系人名单,并将它们发送回服务器。 我建议你​​要保持接触,这是成功地发送到服务器,在本地存储的列表。而接下来当你在通话时间的onChange() contentObserver 的方法,你获取所有联系方式 CONTENT_URI ,其中previously存储列表比较,并只发送更新联系人服务器。

  3:这是我的主要问题,当我进行了更改我的联系人列表中的onChange方法
正在快速连续射击两次。 1变化,2调用。反正是有管理的
本?
 

是这种情况,但我猜你是在错误的IM pression。在我的情况下,它用来烧制随机两次或三次甚至更多次。因此,我不得不设置一个threshold_time间隔。的时间间隔内,如果 ContentObserver 再次被解雇,那么我就不会处理它。我做了这样的事情。

 长lastTimeofCall = 0L;
长lastTimeofUpdate = 0L;
长threshold_time = 10000;

    / *(非Javadoc中)
     * @see android.database.ContentObserver#的onChange(布尔)
     * /
    @覆盖
    公共无效的onChange(布尔selfChange){
        super.onChange(selfChange);

        Log.d(上的变化被称为,内容服务);

        lastTimeofCall = System.currentTimeMillis的();

        如果(lastTimeofCall  -  lastTimeofUpdate> threshold_time){

         //写你的code在这里找到最新的联系人

          lastTimeofUpdate = System.currentTimeMillis的();
        }

    }
 

希望这些建议/解决方案将帮助。

I have an app in which I am hoping to send details in the android contact list to a remote server, so that the user can see his contacts online. To do this I want to notify the remote server of any changes made on the phone to the contacts list.

I have set up a ContentObserver on the 'ContactsContract.Contacts.CONTENT_URI' from a service that gets started when the phone is booted.

I have a number of quiestions, the first 2 are incidental, the third is my major concern.

1: Once I have set up a service that registers a ContentObserver on my Cursor, does that observer only exist within the service? I mean, if the service is killed, does the contentObserver continue to observe?

2: I suspect the answer is no, but I will ask anyway. Is there anyway of knowing which contact being updated is triggering the onchange method of my contentObserver? currently I have to compile the list of all teh contacts on the phone and send them off to my remote server, it would be so much easier to just send details of the contacts being updated.

3: This is my main question, when I make a change to my Contact List the onChange method is being fired twice in quick succession. 1 change, 2 calls. Is there anyway of managing this?

    public class ContactService extends Service {

    JSONArray contactList;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.i("C2DM","content observers initialised");
        super.onCreate();



        //Call Log Content Provider observer

        MyContentContactsObserver contactsObserver = new MyContentContactsObserver();
        ContactService.this.getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contactsObserver);   

    }

    private class MyContentContactsObserver extends ContentObserver {

        public MyContentContactsObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);    
            Log.i("LOG","detected change in contacts: "+selfChange);
        }
    }
}

REsults in 2 quick lines in my logCat:

 detected change in contacts: false 
 detected change in contacts: false

解决方案

i did a similar implementation and had set a ContentObserver over calendar's events URI. and i faced all the problems, you are facing now. So your question with my solutions/suggestions goes here....

1) Once I have set up a service that registers a ContentObserver on my Cursor, 
does that observer only exist within the service? I mean, if the service is killed, 
does the contentObserver continue to observe?  

No, it will not observe the contents your contents over URI. but there is solution to this. you can create an always running service. This service will be created again as soon as it is dismissed due to some memory issue or other unexpected dismissal. It will always continue to run unless it is dismissed explicitly. To create such service override OnStartCommand(Intent intent, int flags, final int startId) in your Service class and return START_STICKY. Have a look at the code below.

public int onStartCommand(Intent intent, int flags, final int startId) {

    String authenticationKey = ((MainApplication) getApplicationContext()).getDbConnector().getUserAuthDefault() ;
    // if user is not registered yet, stop this service, it may be called from boot reciever.
    if(authenticationKey == null){
        stopSelf();
    }

    // restart, if we get killed.
    return START_STICKY;
}  

.

2: I suspect the answer is no, but I will ask anyway. Is there anyway of knowing which
contact being updated is triggering the onchange method of my contentObserver? 
currently I have to compile the list of all teh contacts on the phone and send them off to 
my remote server, it would be so much easier to just send details of the contacts being updated.  

You again guessed it right.:). the answer is No. There is no way to know specifically which contact has been updated. ContentObserver just provide an event to let you know that some change has been made to the data pointed by the uri. But i think you are handling the situation inefficiently, as you are compiling list of all the contacts and sending them back to the server. I would suggest you to maintain a list of contacts, which are successfully sent to server, in the local storage. And next time when you get a call in onChange() method of contentObserver you fetch all contacts from content_URI, compare them with previously stored list and send only updated contacts to server.

3: This is my main question, when I make a change to my Contact List the onChange method 
is being fired twice in quick succession. 1 change, 2 calls. Is there anyway of managing 
this?  

Yes this happens, but i guess you are in wrong impression. In my case it used to fire randomly twice or thrice or even more times. so i had set a threshold_time interval. A time interval within which if ContentObserver is fired again, then i would not process it. I did something like this..

long lastTimeofCall = 0L;
long lastTimeofUpdate = 0L;
long threshold_time = 10000;

    /* (non-Javadoc)
     * @see android.database.ContentObserver#onChange(boolean)
     */
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        Log.d("content service", "on change called");

        lastTimeofCall = System.currentTimeMillis();

        if(lastTimeofCall - lastTimeofUpdate > threshold_time){

         //write your code to find updated contacts here

          lastTimeofUpdate = System.currentTimeMillis();
        }

    }  

Hope these suggestions/solutions would help.