Permission Denial: reading com.android.providers.telephony.SmsProvider uri content: // sms / exception resolution

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44276072/article/details/102695321

Abnormal content

java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/ from pid=24847, uid=10064 requires android.permission.READ_SMS, or grantUriPermission()
                                                    at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
                                                    at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
                                                    at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
                                                    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                    at android.os.Binder.execTransact(Binder.java:453)

Note
sdk 23, in AndroidManifest.xml, it has been configured<uses-permission android:name="android.permission.READ_SMS" />

Analysis
rights issue

Solution
because sdk23 the application permission to make a change, there are two solutions:
1. Use sdk22 develop and run this program android virtual machine or a mobile phone system 6.0.

2. Before using the ContentResolver, enter the following code, checking permissions.The additional code is required to add code * (Line 5, lines 18-24)

public class MainActivity extends AppCompatActivity {
    private TextView tvSms;
    private TextView tvDes;
    private String text = "";
**final private int REQUEST_CODE_ASK_PERMISSIONS = 123;**
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvSms = (TextView) findViewById(R.id.tv_sms);
        tvDes = (TextView) findViewById(R.id.tv_des);
    }
    //点击Button时触发的方法
    public void readSMS(View view) {
        //查询系统信息的uri
        Uri uri = Uri.parse("content://sms/");

        **if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int hasReadSmsPermission = checkSelfPermission(Manifest.permission.READ_SMS);
            if (hasReadSmsPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_PERMISSIONS);
                return;
            }
        }**

            //获取ContentResolver对象
        ContentResolver resolver = getContentResolver();
        //通过ContentResolver对象查询系统短信
        Cursor cursor = resolver.query(uri, new String[]{ "_id","address",
                "type","body", "date"}, null, null, null);
        List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();
        if (cursor != null && cursor.getCount() > 0) {
            tvDes.setVisibility(View.VISIBLE);
            while (cursor.moveToNext()) {
                int _id = cursor.getInt(0);
                String address = cursor.getString(1);
                int type = cursor.getInt(2);
                String body = cursor.getString(3);
                long date = cursor.getLong(4);
                SmsInfo smsInfo = new SmsInfo(_id, address, type, body, date);
                smsInfos.add(smsInfo);
            }
            cursor.close();
        }
        //将查询到的短信内容显示到界面上
        for (int i = 0; i < smsInfos.size(); i++) {
            text += "手机号码:" + smsInfos.get(i).getAddress() + "\n";
            text += "短信内容:" + smsInfos.get(i).getBody() + "\n\n";
            tvSms.setText(text);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44276072/article/details/102695321