Reg : Android 中的联系人更改
我在联系人上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在使用 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....
我假设您希望 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 theContentObserver
and then exits. The service does not need to continuously run as theContentProvider
framework will automatically call your code. Look into registering to receive theBOOT_COMPLETED_ACTION
intent as per this post.