我正在使用一种创建方法来读取来自特定号码的短信,并在我发布的这个问题的帮助下将其加载到列表视图中。特定号码的 SMS 消息未显示在其他 Android 设备上。
方法 getAllSms(getContext()); 不再在 API 级别 28 上调用。我不明白为什么它不再工作,因为我没有改变任何东西,因为它以前工作得很好。即使我在调用方法的位置设置了断点,它也只是被忽略了,但在 API 级别 22 上它完全可以正常工作。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_commands_view, container, false);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED)
{
}
else
{
// Function to load in SMS messages no longer being called on API 28
getAllSms(getContext());
}
return view;
}
@WithPermissions(permissions = {Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS})
@TargetApi(Build.VERSION_CODES.M)
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);
Log.e("Body", body);
Log.e("Date", dateText);
}
}
}