我正在使用 Telephony.Sms 库为我正在开发的应用程序加载接收和发送的短信。当我将查询选择设置为空(查询中的第三项)时,它将显示在我测试过的不同类型的手机上发送和接收的所有短信。
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
但是当我将其设置为特定数字时,在运行 API 27 的三星 S9 手机上,它不会显示任何短信。在 API 23 上运行的 Nexus 上,它将在列表视图中显示接收到的消息,而不是发送的消息。在运行 API 22 的华为手机上,一切正常,显示特定号码的发送和接收消息。
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, sms, null, null);
这是检索特定电话号码发送和接收的短信的完整代码。
@WithPermissions(permissions = {Manifest.permission.READ_SMS})
public void getAllSms(Context context)
{
// Number needs to saved in +614 format
String phoneNumber = SelectedPhNo;
String sms = "address='"+ phoneNumber + "'";
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null , null , null , null); // Sms not showing up on Raza's phone
int totalSms = 0;
String type = null;
if(c != null)
{
totalSms = c.getCount();
if(c.moveToFirst())
{
for(int j = 0; j < totalSms; j++)
{
String smsDate = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.DATE));
String body = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.BODY));
switch(Integer.parseInt(c.getString(c.getColumnIndexOrThrow(Telephony.Sms.TYPE))))
{
case Telephony.Sms.MESSAGE_TYPE_INBOX:
type = "inbox";
break;
case Telephony.Sms.MESSAGE_TYPE_SENT:
type = "sent";
break;
case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
type = "outbox";
break;
default:
break;
}
// Convert smsDate to readable format
Long date = Long.parseLong(smsDate);
// Convert millis value to proper format
Date dateVal = new Date(date);
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
dateText = format.format(dateVal);
//Toast.makeText(context, "Message present", Toast.LENGTH_SHORT).show();
inboxArrayAdapter.add("Command: " + body + "\n" + "Date: "+ dateText);
// Iterate through the list of SMS messages to be displayed in the listview
c.moveToNext();
// Update listview as soon as we receive a new message
((BaseAdapter)inboxmessages.getAdapter()).notifyDataSetChanged();
inboxArrayAdapter.notifyDataSetChanged();;
}
}
}
else
{
Toast.makeText(getContext(), "No Messages found for this contact!", Toast.LENGTH_SHORT).show();
}
}