短信发送观察者执行3次
我已经使用发送消息的观察者定义了以下服务。问题是,当发送消息时,我感觉 contentobserver 的 onChange 方法被调用了 3 次。 ¿有人知道告诉我为什么吗?
谢谢
public class DSMSService extends Service {
private static final String CONTENT_SMS = "content://sms";
private class MyContentObserver extends ContentObserver {
ContentValues values = new ContentValues();
int threadId;
public MyContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.v(TAG, "****************************** SMS change detected *************************************");
Log.v(TAG, "Notification on SMS observer");
// save the message to the SD card here
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);
// this will make it point to the first record, which is the last SMS sent
cur.moveToNext();
String content = cur.getString(cur.getColumnIndex("body"));
Log.v(TAG, "content: " + content);
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.v(TAG, "starting........");
MyContentObserver contentObserver = new MyContentObserver();
ContentResolver contentResolver = getBaseContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, contentObserver);
DAO = new DAOaBlackList(getBaseContext());
}
@Override
public void onDestroy() {
Log.d(TAG, "stopping........");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startid) {
Log.v(TAG, "onStart........");
}
}
I have defined the following service with an observer of messages sent. The problem is that when sending a message, I sense that is called 3 times onChange method of contentobserver. ¿Someone know tell me why?
Thanks
public class DSMSService extends Service {
private static final String CONTENT_SMS = "content://sms";
private class MyContentObserver extends ContentObserver {
ContentValues values = new ContentValues();
int threadId;
public MyContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.v(TAG, "****************************** SMS change detected *************************************");
Log.v(TAG, "Notification on SMS observer");
// save the message to the SD card here
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);
// this will make it point to the first record, which is the last SMS sent
cur.moveToNext();
String content = cur.getString(cur.getColumnIndex("body"));
Log.v(TAG, "content: " + content);
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.v(TAG, "starting........");
MyContentObserver contentObserver = new MyContentObserver();
ContentResolver contentResolver = getBaseContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms"),true, contentObserver);
DAO = new DAOaBlackList(getBaseContext());
}
@Override
public void onDestroy() {
Log.d(TAG, "stopping........");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startid) {
Log.v(TAG, "onStart........");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要做的是检查 onChange 内
content://sms/sent
uri 中最后一项的_id
。您需要存储之前的 _id(可能在静态全局变量中),并在查询content 后将其与光标的最后一项 (
。如果cursor.moveToLast()
) 的 _id 进行比较//sms/已发送_id
相同,您可以选择忽略对onChange的调用。我相信对 onChange 的多次调用是由于在发送过程中短信从一个文件夹移动到另一个文件夹 - 发件箱、已发送项目、一些其他“不可见文件夹”(我们无法确切知道是什么,因为这个特定功能确实需要适当的文档)。由于您无法侦听比content://sms/sent
更具体的 Uri,因此每次您想要检测正在发送的短信时,您都必须对 _id 进行检查。如果前一个
_id
与静态全局变量中的不同,则您将发送一条短信。What you want to do is check for the
_id
of the last item in thecontent://sms/sent
uri inside onChange. You need to store the previous _id (maybe in a static global variable) and compare it to the _id of the last item (cursor.moveToLast()
)of the cursor after you query forcontent://sms/sent
. If the_id
is the same, you can choose to ignore the call to onChange. This multiple calls to onChange I believe is due to the sms being moved from folder to folder during sending - outbox, sent items, some other "invisible folder" (which we can't know exactly what, as this particular feature REALLY REALLY needs proper documentation). As you cannot listen to a more specific Uri thancontent://sms/sent
you'll have to implement this checking for _id everytime you want to detect an sms being sent.If the previous
_id
is different from the one in your static global variable, then you have an sms being sent.您已经通过 URI 保留了 SMS 数据库的观察者。因此,每当发送消息时,数据库都会更新,并且该表的 3 列也会更新。所以它会通知每个人的观察者。因此,随着表数据的更新,它会被调用多次。
You have kept the Observer for the SMS database through URI. so whenever message is being send the database is updated and 3 of the column of that table is getting updated. so it will notify the observer for each of them. so it is being called for as many times as table data is updated.