我使用的是contentObserver
监视sms content provider
的内容多了,我已经把Log.d()
标签进行调试,并在logcat
标签是被视为不止一次意味着onchange()
id被多次调用,我如何防止这种情况发生。我已经在后台运行的服务中实现了观察者。 这里是平变化函数被调用一次
2
A
回答
0
我使用的是内容的观察者观看传出短信,发现该代码
package com.messageHider;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class smsSentService extends Service {
ContentResolver contentResolver;
Uri uri=Uri.parse("content://sms/");
Handler handler;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
contentResolver=getContentResolver();
contentResolver.registerContentObserver(uri, true, new contentObserver(handler));
Log.d("SENTSERVICE", "Service created");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.d("SENTSERVICE", "Service started");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
public class contentObserver extends ContentObserver
{
public contentObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
Cursor cursor=contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();
String type=cursor.getString(cursor.getColumnIndex("type"));
Log.d("THEMESSAGE", type);
super.onChange(selfChange);
}
}
},如果你试图从模拟器发送外发SMS,你居然看到它的3个实例,因为它试图重新发送文本,但失败。你是否也看到了入站文本消息?
如果只是出站SMS消息,请查看SMS状态字段。 -1值表示失败。
0
您需要覆盖deliverSelfNotifications()以返回true。
class contentObserver extends ContentObserver { private Context mContext;
public contentObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
Cursor cursor=contentResolver.query(uri, null, null, null, null);
cursor.moveToFirst();
String type=cursor.getString(cursor.getColumnIndex("type"));
Log.d("THEMESSAGE", type);
super.onChange(selfChange);
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
1
只是一个建议:注册上的onResume方法的内容观察者和注销其上的onPause。
相关问题
- 1. 一次平变化
- 2. 火狐平变化不调用函数
- 3. 平变化函数调用Ajax在firefox
- 4. 函数被调用两次
- 5. jQuery .load()函数只被调用一次
- 6. Javascript keydown函数仅被调用一次
- 7. 。扩展函数只被调用一次
- 8. 函数应该只被调用一次
- 9. 创建一个被一次又一次调用的函数
- 10. 要计数no。次函数被调用?
- 11. 计算函数被调用的次数
- 12. 计算函数被调用的次数
- 13. 函数被调用n + 1次而不是一次 - 为什么?
- 14. 函数被一次点击调用两次
- 15. 每次使用变量时,PHP函数都会被调用吗?
- 16. 构造函数被调用多少次?
- 17. 该函数将被调用多少次?
- 18. Nodejs函数被多次调用
- 19. jQuery - 函数被调用三次
- 20. javascript函数被调用两次
- 21. php析构函数被调用两次
- 22. android webview函数onPagefinished被调用两次
- 23. 函数被调用多次(角JS)
- 24. PageViewController委托函数被调用两次
- 25. Async.waterfall函数被调用两次
- 26. AngularJS函数被调用很多次?
- 27. 构造函数被再次调用?
- 28. AngularJS ngEnter - 函数被调用两次
- 29. 函数被事件调用两次
- 30. 函数是否被多次调用?
我重写deliverSelfNotifications()然后onChange()被调用两次。 – 2012-03-09 05:55:44