- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个应用程序,我希望将 android 联系人列表中的详细信息发送到远程服务器,以便用户可以在线查看他的联系人。为此,我想将手机上对联系人列表所做的任何更改通知远程服务器。
我在“ContactsContract.Contacts.CONTENT_URI”上设置了一个 ContentObserver,该服务在手机启动时启动。
我有几个问题,前两个是偶然的,第三个是我主要关心的问题。
1:一旦我设置了一个在我的光标上注册 ContentObserver 的服务,该观察者是否只存在于服务中?我的意思是,如果服务被杀死,contentObserver 会继续观察吗?
2:我怀疑答案是否定的,但我还是会问。是否知道正在更新的联系人正在触发我的 contentObserver 的 onchange 方法?目前我必须编译手机上所有联系人的列表并将它们发送到我的远程服务器,发送正在更新的联系人的详细信息会容易得多。
3:这是我的主要问题,当我对联系人列表进行更改时,onChange 方法会快速连续触发两次。 1 次更改,2 次调用。有没有办法管理这个?
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);
}
}
}
在我的 logCat 中快速生成 2 行:
detected change in contacts: false
detected change in contacts: false
最佳答案
我做了一个类似的实现,并在日历的事件 URI
上设置了一个 ContentObserver
。我面临着所有的问题,你现在面临着。所以你对我的解决方案/建议的问题就在这里......
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?
不,它不会通过 URI 观察您的内容。但有解决办法。您可以创建一个始终运行的服务。由于某些内存问题或其他意外解除,该服务将在被解除后立即重新创建。除非明确解除,否则它将始终继续运行。要在您的服务类中创建此类服务覆盖 OnStartCommand(Intent intent, int flags, final int startId)
并返回 START_STICKY
。看看下面的代码。
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.
你又猜对了。:)。答案是否定的。
无法具体知道哪个联系人已更新。 ContentObserver 只是提供一个事件,让您知道 uri 指向的数据发生了一些变化。但是我认为您处理这种情况的效率很低,因为您正在编译所有联系人的列表并将它们发送回服务器。
我建议您在本地存储中维护一个成功发送到服务器的联系人列表。下次当您在 contentObserver
的 onChange()
方法中接到电话时,您会从 content_URI
获取所有 contacts
,将它们与先前存储的列表进行比较,并仅将更新的联系人发送到服务器。
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?
是的,这会发生,但我猜你的印象是错误的。就我而言,它曾经随机发射两次或三次甚至更多次。所以我设置了一个 threshold_time 间隔。如果再次触发 ContentObserver
的时间间隔,那么我不会处理它。我做了这样的事情..
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();
}
}
希望这些建议/解决方案会有所帮助。
关于java - Content Observer onChange 方法在 1 次光标更改后调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10173996/
一段时间后,我阅读了有关 RxJava concat 的内容,并决定测试一下我的理解力。但是我遇到了一些我不太理解的行为。 问题是,当我连接两个可观察对象时,根据我将它们传递给 Observable.
我正在使用来自数据库服务的数据实现自动完成: @Injectable() export class SchoolService { constructor(private db: AngularF
我正在尝试使用 RxJS 创建一个可观察的对象,它可以执行如图所示的操作。 获取一个值并等待一段固定的时间才能获得 下一个。 下一个将是该周期内发出的最后一个值 等等,跳过其余部分。 如果等待时间间隔
我有一个可观察对象和另一个提供的可观察对象改变 key 。我想构建一个在之间切换的可观察对象基于该键的对象中的可观察值。 示例: // Choose randomly between "up" or
我使用 protobuffers 在我的前端和我的 Dart 服务器之间进行通信。 那些对象没有实现 Observable . 我的 Dart 聚合物对象看起来像: @CustomTag('user-
在 java swing 项目中,我有一个模型类,它保存某个 JPanel 的状态。我需要使这些数据可供 View 使用。我认为有两种选择。有一个扩展 Observable 的类并将模型作为实例变量。
我想找到一种方法来检测观察者是否已完成使用我使用 Rx.Observable.create 创建的自定义可观察对象,以便自定义可观察对象可以结束它并正确地进行一些清理。 因此,我创建了一些测试代码,如
我正在尝试查询数据库。迭代结果列表,并为每一项再执行一个请求。在 rxjs 构建结束时,我有 Observable[]> 。但我需要Observable 。如何做到这一点? this.caseServ
我希望我的 api 上有一个方法返回 Observable> 但我希望该方法中的代码知道所有包含的 Observables 是否已完成,以便它可以关闭某些内容。最好的方法是什么? 更明确地说,我希望完
我有两个方法返回 Observable> firstAPI.getFirstInfo("1", "2"); secondApi.getSecondInfo(resultFirstObservable,
我有一个 Observable返回单个 Cursor实例(Observable)。我正在尝试利用 ContentObservable.fromCursor获取 onNext 中每个游标的行回调。 我想
我有两种返回 Observable 的方法: Observable firstObservable(); Observable secondObservable(String value); 对于第一
我正在尝试创建一个将用户数据作为 Observable 的函数,并使用来自第一个 observable 的数据从查询中添加/合并数据,然后将所有这些数据作为一个 observable 返回,我可以这样
我有一个 spec-compliant ECMAScript Observable ,具体来自 wonka library .我正在尝试将这种类型的 observable 转换为 rxjs 6 obs
为了简化问题,我在这里使用了数字和字符串。代码: const numbers$:Observable = of([1,2,3]); const strings: string[] = ["a","b"
对于我的 Android 应用程序,我需要一个 Observable 来聚合来自 7 个不同搜索的结果并作为一个集合发出。 对于最终发射,我选择了 ListMultimap其中 Content是搜索结
我正在使用改造 2.0.0-beta2 并且调试构建工作正常,但我在使用 Proguard 发布构建时遇到以下错误。 这是更新后的 logcat 错误。 11-17 18:23:22.751 1627
observer.throw(err) 和 observer.error(err) 有什么区别? 我正在使用 RxJS 版本“5.0.0-beta.12” var innerObservable =
我们有一种情况,对服务的方法调用返回一个 IObservable但我们的客户期望 IObservable .将 T1 转换为 T2 很简单。 Rx 中有什么允许这样做的吗? (即链接观察者) 我知道我
我陷入了如何将以下可观察类型转换/转换为我的目标类型的困境: 我有可观察的类型: Observable>> 我想将其转换为: Observable> 所以当我订阅它时,它会发出 List不是Obser
我是一名优秀的程序员,十分优秀!