听传出短信或发件箱 [英] Listen to outgoing SMS or sent box
问题描述
我开发,将存储所有传入和传出的短信在SD卡上的文本文件的应用程序。
I am developing an application which will store all the incoming and outgoing sms in a text file in SD card.
我能听使用广播接收传入的消息。我发现很难听传出的短信。
I am able to listen incoming messages using broadcast receiver. I am finding it very difficult to listen to outgoing SMS.
我知道,在一定程度上,需要在发件箱或者发件箱内容观察员进行设置,但我不知道该怎么做。
I know to some extent that a content observer on the sent box or outbox needs to be set, but I don't know how to do it.
如何才能做到这一点?
推荐答案
基本上,你必须注册内容的观察者......是这样的:
Basically, you have to register a content observer... something like this:
ContentResolver contentResolver = context.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, yourObserver);
yourObserver
是一个对象(新YourObserver(新处理器())
)可能是这样的:
yourObserver
is an object (new YourObserver(new Handler())
) that could look like this:
class YourObserver extends ContentObserver {
public YourObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
}
}
那么,究竟你明白了短信的内容?您必须使用光标
:
// save the message to the SD card here
Uri uriSMSURI = Uri.parse("content://sms/out");
Cursor cur = this.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"));
// use cur.getColumnNames() to get a list of all available columns...
// each field that compounds a SMS is represented by a column (phone number, status, etc.)
// then just save all data you want to the SDcard :)
这篇关于听传出短信或发件箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!