我已经定义了以下服务,发送消息的观察者。的问题是,当发送消息时,我感觉到被称为3倍的onChange contentobserver的方法。 ¿有人知道的告诉我为什么?
感谢
公共类DSMSService延伸服务{
私有静态最后弦乐CONTENT_SMS =内容://短信;
私有类MyContentObserver扩展ContentObserver {
ContentValues值=新ContentValues();
INT主题ID;
公共MyContentObserver(){
超(空);
}
@覆盖
公共无效的onChange(布尔selfChange){
super.onChange(selfChange);
Log.v(TAG,*********短信的变化检测********** ************);
Log.v(TAG,短信观察者通知);
//消息保存到SD卡在这里
乌里uriSMSURI = Uri.parse(内容:// SMS);
光标CUR = getBaseContext()getContentResolver()查询(uriSMSURI,NULL,NULL,NULL,NULL);
//这将使它指向第一个记录,这是最后发送的短信
cur.moveToNext();
字符串的内容= cur.getString(cur.getColumnIndex(身体));
Log.v(TAG的内容:+内容);
}
@覆盖
公共布尔deliverSelfNotifications(){
返回false;
}
}
@覆盖
公众的IBinder onBind(意向意图){
返回null;
}
@覆盖
公共无效的onCreate(){
Log.v(TAG,开始........);
MyContentObserver contentObserver =新MyContentObserver();
ContentResolver的ContentResolver的= getBaseContext()getContentResolver()。
contentResolver.registerContentObserver(Uri.parse(内容://短信),真实,contentObserver);
DAO =新DAOaBlackList(getBaseContext());
}
@覆盖
公共无效的onDestroy(){
Log.d(TAG,停止........);
}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
Log.v(TAG,接收开始编号+ startId +:+意图);
//我们希望这项服务继续运行,直到它被明确
//停止,因此返回粘。
返回START_STICKY;
}
@覆盖
公共无效ONSTART(意向意图,诠释startid){
Log.v(TAG,ONSTART ........);
}
}
解决方案
抱歉这么晚才回答,我想我贴我的回答较早,但显然事实并非如此。
您想要做的是检查的 _id
在内容的最后一个项目://短信/发送
URI内的onChange。你需要存储previous _id(也许在一个静态全局变量),并把它比作(()
cursor.moveToLast)的最后一个项目的_id光标之后您查询内容://短信/发送
。如果 _id
是一样的,你可以选择忽略调用的onChange。这多次调用的onChange我认为这是由于短信从文件夹移动过程中发送到文件夹 - 发件箱,已发送的邮件,其他一些看不见的文件夹(这是我们不能确切地知道,因为这个特殊的功能,真的真的需要适当文档)。正如你可以不听一个更具体的乌里比内容://短信/发送
你必须实现这个检查要检测发送短信_id每次。
如果在previous _id
是一个在你的静态全局变量不同,那么你有发送短信。
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........");
}
}
解决方案
Sorry for the late answer, I thought I posted my answer earlier but apparently it wasn't.
What you want to do is check for the _id
of the last item in the content://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 for content://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 than content://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.