特定号码的短信未显示在其他设备上 Android
SMS Messages of a particular number not showing up on other devices Android
我正在使用 Telephony.Sms 库加载我正在使用的应用程序接收和发送的短信。当我将查询选择设置为 null(查询中的第三项)时,它将显示我一直在测试的不同类型的 phones 上所有已发送和已接收的短信。
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
但是当我将它设置为特定号码时,在三星 S9 phone 运行 和 API 27 上它没有显示任何短信。在 API 23 上运行的 Nexus 上,它将在列表视图中显示收到的消息但不显示发送的消息。在华为phone 运行 API 22上,一切正常,显示特定号码的发送和接收消息。
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, sms, null, null);
这是检索特定 phone 号码发送和接收的短信的完整代码。
@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();
}
}
查询 SMS/MMS 消息非常棘手,并且在不同的 Android 版本和不同的制造商之间差异很大。
这是应该在所有 Android K+ 设备上正常工作的版本:
HashSet<String> phonesSet = new HashSet<>();
phonesSet.add(phoneNumber);
long threadId = Threads.getOrCreateThreadId(context, phonesSet); // get the thread-id of the specific conversation thread
Uri threadUri = ContentUris.withAppendedId(Threads.CONTENT_URI, threadId); // get the thread-uri
String[] projection = new String[]{MmsSms.TYPE_DISCRIMINATOR_COLUMN, BaseColumns._ID, Conversations.THREAD_ID,
Telephony.Sms.ADDRESS, Telephony.Sms.BODY, "sort_index", Telephony.Sms.DATE_SENT,
Telephony.Sms.READ, Telephony.Sms.TYPE, Telephony.Sms.STATUS, Telephony.Sms.LOCKED,
Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN};
Cursor cur = getContentResolver().query(threadUri, projection, null, null, null);
DatabaseUtils.dumpCursor(cur);
这是能够在各种 android 设备上获取 Sent/Received SMS 消息的完整代码解决方案。这已经在不同 android 设备(包括华为、Oppo 和三星)的 API 级别 22、23、26 和 28 上进行了测试。
public void getAllSms(Context context)
{
HashSet<String> phoneSet = new HashSet<>();
phoneSet.add(SelectedPhNo); // phoneNumber
long threadId = Telephony.Threads.getOrCreateThreadId(context, phoneSet);
Uri threadUri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);
String[] projection = new String[] {Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN, BaseColumns._ID, Telephony.Sms.Conversations.THREAD_ID,
Telephony.Sms.ADDRESS, Telephony.Sms.BODY, "sort_index", Telephony.Sms.DATE_SENT, Telephony.Sms.DATE,
Telephony.Sms.READ, Telephony.Sms.TYPE, Telephony.Sms.STATUS, Telephony.Sms.LOCKED,
Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN, Telephony.Sms.Inbox.BODY, Telephony.Sms.Sent.BODY};
Cursor cur = context.getContentResolver().query(threadUri, projection, null, null, "normalized_date desc");
DatabaseUtils.dumpCursor(cur);
// Read cursor into an arraylist
ArrayList<String> mArrayList = new ArrayList<String>();
int totalSms = cur.getCount();
if(cur.moveToFirst())
{
for(int i = 0; i < totalSms; i++)
{
String body = cur.getString(cur.getColumnIndex(Telephony.Sms.BODY));
String indexDate = cur.getString(cur.getColumnIndex(Telephony.Sms.DATE));
// Convert string to long variable
Long date = Long.parseLong(indexDate);
// Convert millis value to proper format
Date dateVal = new Date(date);
//"dd-MMM-yyyy""dd/MM/yyyy"
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
dateText = format.format(dateVal);
cur.moveToNext();
inboxArrayAdapter.add("Command: " + body + "\n" + "Date: " + dateText);
}
}
}
我正在使用 Telephony.Sms 库加载我正在使用的应用程序接收和发送的短信。当我将查询选择设置为 null(查询中的第三项)时,它将显示我一直在测试的不同类型的 phones 上所有已发送和已接收的短信。
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
但是当我将它设置为特定号码时,在三星 S9 phone 运行 和 API 27 上它没有显示任何短信。在 API 23 上运行的 Nexus 上,它将在列表视图中显示收到的消息但不显示发送的消息。在华为phone 运行 API 22上,一切正常,显示特定号码的发送和接收消息。
Cursor c = cr.query(Telephony.Sms.CONTENT_URI, null, sms, null, null);
这是检索特定 phone 号码发送和接收的短信的完整代码。
@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();
}
}
查询 SMS/MMS 消息非常棘手,并且在不同的 Android 版本和不同的制造商之间差异很大。
这是应该在所有 Android K+ 设备上正常工作的版本:
HashSet<String> phonesSet = new HashSet<>();
phonesSet.add(phoneNumber);
long threadId = Threads.getOrCreateThreadId(context, phonesSet); // get the thread-id of the specific conversation thread
Uri threadUri = ContentUris.withAppendedId(Threads.CONTENT_URI, threadId); // get the thread-uri
String[] projection = new String[]{MmsSms.TYPE_DISCRIMINATOR_COLUMN, BaseColumns._ID, Conversations.THREAD_ID,
Telephony.Sms.ADDRESS, Telephony.Sms.BODY, "sort_index", Telephony.Sms.DATE_SENT,
Telephony.Sms.READ, Telephony.Sms.TYPE, Telephony.Sms.STATUS, Telephony.Sms.LOCKED,
Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN};
Cursor cur = getContentResolver().query(threadUri, projection, null, null, null);
DatabaseUtils.dumpCursor(cur);
这是能够在各种 android 设备上获取 Sent/Received SMS 消息的完整代码解决方案。这已经在不同 android 设备(包括华为、Oppo 和三星)的 API 级别 22、23、26 和 28 上进行了测试。
public void getAllSms(Context context)
{
HashSet<String> phoneSet = new HashSet<>();
phoneSet.add(SelectedPhNo); // phoneNumber
long threadId = Telephony.Threads.getOrCreateThreadId(context, phoneSet);
Uri threadUri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);
String[] projection = new String[] {Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN, BaseColumns._ID, Telephony.Sms.Conversations.THREAD_ID,
Telephony.Sms.ADDRESS, Telephony.Sms.BODY, "sort_index", Telephony.Sms.DATE_SENT, Telephony.Sms.DATE,
Telephony.Sms.READ, Telephony.Sms.TYPE, Telephony.Sms.STATUS, Telephony.Sms.LOCKED,
Telephony.Sms.ERROR_CODE, Telephony.Sms.SEEN, Telephony.Sms.Inbox.BODY, Telephony.Sms.Sent.BODY};
Cursor cur = context.getContentResolver().query(threadUri, projection, null, null, "normalized_date desc");
DatabaseUtils.dumpCursor(cur);
// Read cursor into an arraylist
ArrayList<String> mArrayList = new ArrayList<String>();
int totalSms = cur.getCount();
if(cur.moveToFirst())
{
for(int i = 0; i < totalSms; i++)
{
String body = cur.getString(cur.getColumnIndex(Telephony.Sms.BODY));
String indexDate = cur.getString(cur.getColumnIndex(Telephony.Sms.DATE));
// Convert string to long variable
Long date = Long.parseLong(indexDate);
// Convert millis value to proper format
Date dateVal = new Date(date);
//"dd-MMM-yyyy""dd/MM/yyyy"
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
dateText = format.format(dateVal);
cur.moveToNext();
inboxArrayAdapter.add("Command: " + body + "\n" + "Date: " + dateText);
}
}
}