gpt4 book ai didi

android - 在 Android 中收听传出的短信或已发送的邮箱

转载 作者:IT老高 更新时间:2023-10-28 21:46:23 24 4
gpt4 key购买 nike

我正在开发一个应用程序,它将所有传入和传出的短信存储在 SD 卡中的文本文件中。

我可以使用广播接收器收听传入的消息。我发现很难收听传出的短信。

我在某种程度上知道需要设置已发送箱或发件箱上的内容观察器,但我不知道该怎么做。

如何做到这一点?

最佳答案

基本上,你必须注册一个内容观察者......像这样:

ContentResolver contentResolver = context.getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, yourObserver);

yourObserver 是一个对象 (new YourObserver(new Handler())),可能如下所示:

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
}
}

那么,您究竟是如何获得短信内容的呢?您必须使用 Cursor:

// 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 :)

关于android - 在 Android 中收听传出的短信或已发送的邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5808577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com