Reg : Android 中的联系人更改

发布于 2024-08-31 00:57:12 字数 724 浏览 4 评论 0原文

我在联系人上使用 ContentObserver。但这里我的问题是至少一旦我必须启动我的应用程序,否则我无法收到通知更改。我的代码像这样

ContactsContentObserver cco = new ContactsContentObserver(handler);

    ContentResolver contentResolver = getContentResolver();

    contentResolver.registerContentObserver(RawContacts.CONTENT_URI,
    true, cco);

            }

private class ContactsContentObserver extends ContentObserver 
{
 public ContactsContentObserver(Handler h) 
 {
      super(h);
    }

    public void onChange(boolean selfChange) 
    {
       System.out.println("##########SOMEBODAY CHANGED ANYTHING AT THE CONTACTS");
       Toast.makeText(getApplication(),"####Updated####",Toast.LENGTH_LONG).show();


       }

.... 高级谢谢。

I am using ContentObserver on contacts. But here my problem is atleast once i have to launch my application other wise i am unable to get notification chages. My code like this

ContactsContentObserver cco = new ContactsContentObserver(handler);

    ContentResolver contentResolver = getContentResolver();

    contentResolver.registerContentObserver(RawContacts.CONTENT_URI,
    true, cco);

            }

private class ContactsContentObserver extends ContentObserver 
{
 public ContactsContentObserver(Handler h) 
 {
      super(h);
    }

    public void onChange(boolean selfChange) 
    {
       System.out.println("##########SOMEBODAY CHANGED ANYTHING AT THE CONTACTS");
       Toast.makeText(getApplication(),"####Updated####",Toast.LENGTH_LONG).show();


       }

....
Adv thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不疑不惑不回忆 2024-09-07 00:57:12

如果您正在使用 Android 开发 UI 应用程序,那么无论您的代码或功能如何,只有在启动应用程序后才会起作用。
开发服务或 Beckgroud 进程的更好选择,它将在设备后台运行。您甚至不需要启动它。
您正在使用 Android 屏幕键盘的实例。
这是一种等待某种事件或事件并基于该事件弹出的应用程序。
尝试开发服务,看看会发生什么......

IF you are developing an UI application with Android then whatever your code or functionality will only works once you launch your application.
The better option for you to develop the service or Beckgroud process, which will be running in background on the device. And you dont even need to launch it.
You are having an live example of OnScreen Keyboard of Android.
That is one kind of application which wait for some sort of event or something and based on that it pop's up.
Try to develop service and see what happens....

楠木可依 2024-09-07 00:57:12

我假设您希望 ContentObserver 在联系人发生变化时自动运行。问题是,需要调用注册该侦听器的代码。您可能可以做的是创建一个服务,该服务在设备完成启动时启动,注册 ContentObserver 然后退出。该服务不需要持续运行,因为 ContentProvider 框架会自动调用您的代码。根据 这篇文章。

I assume you are wanting the ContentObserver to automatically run when something changes in the contacts. The problem is, something will need to call the code that registers that listener. What you probably could do is create a service that starts when the device finishes booting, registers the ContentObserver and then exits. The service does not need to continuously run as the ContentProvider framework will automatically call your code. Look into registering to receive the BOOT_COMPLETED_ACTION intent as per this post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文