在棉花糖中检索短信

Retrieving SMS in marshmallow

我的本机收件箱中有一条失败的短信。我无法从我的应用程序中检索它。我使用的查询是:

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

其中 73 是线程 ID。在 lollipop 和 kitkat 中,它工作正常。但在棉花糖中,它 returns “0” 条新消息。

请帮帮我。

marshmallow 中的原生 SMS DB 参数是否有任何变化???

你能试试这个 uri : content://sms/inbox

出现此问题是因为在 marshmallow 中,查看 SMS 的访问权限受到限制。创建了名为 "sms_restricted" 的 SQL 视图,它仅包含 MESSAGE_TYPE_INBOX 和 MESSAGE_TYPE_SENT。这是单独暴露给其他应用程序开发人员的。

失败消息、草稿消息、发件箱消息、排队消息无法访问,除非我们的应用程序是 "Default Messaging application"。

在棉花糖中,获取权限应该是运行时

   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;

    }
}