实例详解Android 获取短信会话列表
为什么需要获取短信会话列表
在我们的日常生活中需要经常使用手机发送和接收短信,而对于一些需要短信的应用程序来说,获取短信会话列表是很有必要的,比如备份短信、导出短信等等。因此,本文将介绍获取Android短信会话列表的方法。
实现步骤
1. 添加权限
首先,在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.READ_SMS"/>
2. 获取短信列表
接下来,我们需要使用ContentResolver
来获取短信列表:
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/conversations"), null, null, null, "thread_id DESC");
其中,Uri.parse("content://sms/conversations")
表示获取短信会话列表,thread_id DESC
表示按短信会话的时间降序排序。
3. 遍历短信列表
我们可以使用循环遍历Cursor对象,来获取短信会话列表中的每个会话:
if (cursor != null && cursor.moveToFirst()) {
do {
long threadId = cursor.getLong(cursor.getColumnIndex("thread_id"));
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
int unreadCount = cursor.getInt(cursor.getColumnIndex("unread_count"));
// 获取该会话的最新短信
Cursor msgCursor = getContentResolver().query(Uri.parse("content://sms"), null, "thread_id = " + threadId, null, "date DESC LIMIT 1");
if (msgCursor != null && msgCursor.moveToFirst()) {
long msgId = msgCursor.getLong(msgCursor.getColumnIndex("_id"));
long date = msgCursor.getLong(msgCursor.getColumnIndex("date"));
// TODO: 处理获取到的短信会话信息
}
if (msgCursor != null) {
msgCursor.close();
}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
4. 根据需要进行处理
最后,根据我们的需求,对获取到的短信会话信息进行处理,比如打印、显示在列表中等。
示例说明
示例1:获取短信会话列表
我们可以在一个新的Activity中实现上述步骤:
public class SmsListActivity extends AppCompatActivity {
private static final String TAG = "SmsListActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_list);
// 添加权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, 0);
// 获取短信会话列表
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/conversations"), null, null, null, "thread_id DESC");
// 遍历短信列表
if (cursor != null && cursor.moveToFirst()) {
do {
long threadId = cursor.getLong(cursor.getColumnIndex("thread_id"));
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
int unreadCount = cursor.getInt(cursor.getColumnIndex("unread_count"));
// 获取该会话的最新短信
Cursor msgCursor = getContentResolver().query(Uri.parse("content://sms"), null, "thread_id = " + threadId, null, "date DESC LIMIT 1");
if (msgCursor != null && msgCursor.moveToFirst()) {
long msgId = msgCursor.getLong(msgCursor.getColumnIndex("_id"));
long date = msgCursor.getLong(msgCursor.getColumnIndex("date"));
Log.d(TAG, "ThreadId: " + threadId + ", Address: " + address + ", Body: " + body + ", UnreadCount: " + unreadCount + ", MsgId: " + msgId + ", Date: " + date);
}
if (msgCursor != null) {
msgCursor.close();
}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
}
}
示例2:显示短信会话列表
我们可以在ListView中显示短信会话列表:
public class SmsListActivity extends AppCompatActivity {
private static final String TAG = "SmsListActivity";
private ArrayList<SmsConversations> smsConversations = new ArrayList<>();
private ListView smsListView;
private SmsListAdapter smsListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_list);
// 添加权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, 0);
// 获取短信会话列表
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/conversations"), null, null, null, "thread_id DESC");
// 遍历短信列表
if (cursor != null && cursor.moveToFirst()) {
do {
SmsConversations sms = new SmsConversations();
long threadId = cursor.getLong(cursor.getColumnIndex("thread_id"));
String address = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
int unreadCount = cursor.getInt(cursor.getColumnIndex("unread_count"));
// 获取该会话的最新短信
Cursor msgCursor = getContentResolver().query(Uri.parse("content://sms"), null, "thread_id = " + threadId, null, "date DESC LIMIT 1");
if (msgCursor != null && msgCursor.moveToFirst()) {
long msgId = msgCursor.getLong(msgCursor.getColumnIndex("_id"));
long date = msgCursor.getLong(msgCursor.getColumnIndex("date"));
sms.setThreadId(threadId);
sms.setAddress(address);
sms.setBody(body);
sms.setUnreadCount(unreadCount);
sms.setMsgId(msgId);
sms.setDate(date);
smsConversations.add(sms);
}
if (msgCursor != null) {
msgCursor.close();
}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
// 显示短信会话列表
smsListView = findViewById(R.id.sms_list);
smsListAdapter = new SmsListAdapter(this, smsConversations);
smsListView.setAdapter(smsListAdapter);
}
}
其中,SmsConversations是一个实体类,表示短信会话信息;SmsListAdapter是自定义适配器。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:实例详解Android 获取短信会话列表 - Python技术站