【发布时间】:2016-04-13 22:38:36
【问题描述】:
我可以使用 TelephonyManager 获取用户的电话接线员,并且我需要使用此名称来检索从接线员处收到的文本。如何在 Android 的 ContextResolver.query() 方法中使用 SQL where 子句之类的东西?这是我检索用户收到的所有短信的代码。我需要调整它以从用户的电话接线员那里检索消息。
public void fetchSMS() {
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String operator=tMgr.getNetworkOperatorName();
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] cols = new String[]{"_id", "thread_id", "address", "person", "date", "body", "type"};
Cursor cursor1 = getContentResolver().query(
mSmsinboxQueryUri,
cols, null, null, null);
if (cursor1!=null && cursor1.moveToFirst()) {
do {
String address = cursor1.getString(cursor1
.getColumnIndex(cols[2]));
//String address2=(address.split("-"))[1];
String date = cursor1.getString(cursor1
.getColumnIndex(cols[4]));
String msg = cursor1.getString(cursor1
.getColumnIndex(cols[5]));
String type = cursor1.getString(cursor1
.getColumnIndex(cols[6]));
smsLog.append("\n" + "Address:").append(address).append("\n" + "Date:").append(date).append("\n" + "Message:").append(msg).append("\n" + "Type:").append(type).append("\n\n");
cursor1.moveToNext();
}
while (!cursor1.isAfterLast());
cursor1.close();
}
}
【问题讨论】:
-
除非您知道任何给定运营商可以从其发送 SMS 的每个地址,否则您将无法做到这一点 - 无论如何是可靠的。 SMS 表中没有包含运营商名称的列,并且无法保证来自运营商的消息将在消息正文中包含其名称。
-
当发件人在充值后收到他/她的电信运营商的消息(预付费电话)时,他们会收到来自特定地址的短信(我的电话运营商叫Docomo,我收到的消息有发件人为 TA-Docomo)。其他运营商也一样。我只需要知道如何在 query() 方法中添加 where 子句。
-
嗯,不能保证每个人都这样,但如果您只想要一个简单的 where 子句,您的查询将类似于:
query(mSmsinboxQueryUri, cols, "address LIKE '%" + operator + "%'", null, null)。 -
非常感谢!这很有帮助! :)
标签: android