Ajitha
Ajitha

Reputation: 707

Retrieving SMS in marshmallow

I have a failed SMS in my native inbox. I m not able to retrieve it from my application. The query I use is:

content://mms-sms/conversations/73

SELECT transport_type, _id, thread_id, address, body, date, date_sent, read, type, status, locked, error_code, sub, sub_cs, date, date_sent, read, m_type, msg_box, d_rpt, rr, err_type, locked, st FROM (SELECT DISTINCT date * 1 AS normalized_date, 'sms' AS transport_type, _id, thread_id, address, body, date, date_sent, read, type, status, locked, error_code, NULL AS sub, NULL AS sub_cs, date, date_sent, read, NULL AS m_type, NULL AS msg_box, NULL AS d_rpt, NULL AS rr, NULL AS err_type, locked, NULL AS st FROM sms WHERE (thread_id = 73 AND (type != 3)) UNION SELECT DISTINCT date * 1000 AS normalized_date, 'mms' AS transport_type, pdu._id, thread_id, NULL AS address, NULL AS body, date, date_sent, read, NULL AS type, NULL AS status, locked, NULL AS error_code, sub, sub_cs, date, date_sent, read, m_type, msg_box, d_rpt, rr, err_type, locked, st FROM pdu LEFT JOIN pending_msgs ON pdu._id = pending_msgs.msg_id WHERE (thread_id = 73 AND msg_box != 3 AND (msg_box != 3)) ORDER BY normalized_date ASC) ORDER BY normalized_date ASC

where 73 is the thread ID. In lollipop and kitkat , it works fine. But in marshmallow, it returns "0" new messages.

Please help me here.

Was there any changes in native SMS DB params in marshmallow???

Upvotes: 1

Views: 1501

Answers (3)

GomathiSelvakumar
GomathiSelvakumar

Reputation: 494

in marshmallow,get permission should be runtime

   final private int REQUEST_CODE_ASK_PERMISSIONS = 123;       


   if(Build.VERSION.SDK_INT < 23){
         //your code here
     }else {
        requestContactPermission();
    } 



    private void requestContactPermission() {

    int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.READ_SMS);

    if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
        ActivityCompat.requestPermissions(Context, new String[]{Manifest.permission.READ_SMS}, PERMISSION_REQUEST_CODE);
    }else {
        //Toast.makeText(AddContactsActivity.this, "Contact Permission is already granted", Toast.LENGTH_LONG).show();
         }
    }


   @Override
   public void onRequestPermissionsResult(int requestCode, String[]       permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            // Check if the only required permission has been granted
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Log.i("Permission", "Contact permission has now been granted. Showing result.");
                Toast.makeText(this,"Contact Permission is Granted",Toast.LENGTH_SHORT).show();
            } else {
                Log.i("Permission", "Contact permission was NOT granted.");
                } 

             break;

    }
}

Upvotes: 2

Ajitha
Ajitha

Reputation: 707

This issue occurs because in marshmallow, there is a restricted access in viewing SMS. A SQL view is created named "sms_restricted" and it contains only MESSAGE_TYPE_INBOX and MESSAGE_TYPE_SENT. That is alone exposed to other application developers.

The failed messages, draft messages, outbox messages, queued messages are not accessible unless our application is made as "Default Messaging application".

Upvotes: 3

Febi M Felix
Febi M Felix

Reputation: 2849

Could you please try with this uri : content://sms/inbox

Upvotes: 0

Related Questions