0

I was using a method created to read in sms messages from a particualr number and load it into a listview with the help from this question that I posted. SMS Messages of a particular number not showing up on other devices Android.

The method getAllSms(getContext()); is no longer being called on API Level 28. I don't understand why it's no longer working because I haven't changed anything because it was working completely fine before. Even if I put a breakpoint on where the method is being called, it's just being ignored, but on API level 22 it's working completely fine.

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);
            }
        }
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Sahil Bora
  • 173
  • 1
  • 12
  • Please take a look at SMS retriever API. It restrict you getting access to SMS unless your app satisfy certain criterias. I think this may be the issue. – mnp343 May 17 '19 at 02:03
  • The code for retrieving the SMS was working fine before. Is it something to do with the compilier or API version. All required permissions for reading sms have been enabled. – Sahil Bora May 17 '19 at 02:06
  • Yup it has deal with API version. – mnp343 May 17 '19 at 02:07
  • What I don't understand is that is exact code was working completely fine the other day, it's just when I recomplied it, the method was no longer being called. – Sahil Bora May 17 '19 at 02:08
  • New restrictions are imposed to stop apps accessing all SMS unless it is selected as default SMS app or fall in the criterias decided. – mnp343 May 17 '19 at 02:10
  • https://android-developers.googleblog.com/2019/01/reminder-smscall-log-policy-changes.html?m=1 – mnp343 May 17 '19 at 02:11
  • So I'll need to write extra code to get around this to be able to read sms messages from the inbox – Sahil Bora May 17 '19 at 02:13
  • Unless you fall in the criterias mentioned or you can read just otp msgs sent with predefined format. – mnp343 May 17 '19 at 02:15
  • Could it be also to do with min Sdk Version because I currently have it set at 22 but those runtime permissions are in API 23 – Sahil Bora May 17 '19 at 02:31

0 Answers0