我写了一个应用程序来显示发送的短信。但是我的内容观察者没有被以下代码触发,当从默认消息传递应用程序发送SMS而不是使用URI“content:// sms”触发时。
SMSSentObserver smsSentObserver = new SMSSentObserver(null, this);
ContentResolver contentResolver = this.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/sent"), true, smsSentObserver);
有人可以告诉我为什么?我已经添加了READ_SMS的权限。
<uses-permission android:name="android.permission.READ_SMS" />
基本上我需要保存在我的应用程序上发送的短信。任何帮助表示赞赏。
编辑:
我尝试添加“android.permission.SEND_SMS”作为权限
这是我的SMSSentObserver:
public class SMSSentObserver extends ContentObserver {
private final Context _context;
public SMSSentObserver(Handler handler, Context context) {
super(null);
_context = context;
}
@Override
public void onChange(boolean selfChange) {
saveSentSMS();
super.onChange(selfChange);
}
private void saveSentSMS() {
Uri uriSMSURI = Uri.parse("content://sms/sent");
Cursor cur = this._context.getContentResolver().query(uriSMSURI, null, null, null, null);
if(null == cur) {
return;
}
if(cur.moveToFirst()) {
String strProtocol = cur.getString(cur.getColumnIndex("protocol"));
if(null != strProtocol) { //for sent messages protocol will be null.
return;
}
int type = cur.getInt(cur.getColumnIndex("type"));
if(type == 2) {
String strSMSBody = cur.getString(cur.getColumnIndex("body"));
String strPhoneNo = cur.getString(cur.getColumnIndex("address"));
long smsDateTime = cur.getLong(cur.getColumnIndex("date"));
String serviceCenter = cur.getString(cur.getColumnIndex("service_center"));
String strStatus = cur.getString(cur.getColumnIndex("status"));
String strPerson = cur.getString(cur.getColumnIndex("person"));
Log.e("Info", "SMS saved");
}
}
}
}
谢谢