Java – why does Android ignore read_ SMS permissions?

I played and read my inbox under Android API 15, and I encountered the following problems:

My application has only one activity. The main activity is started by default. It has oncreate code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unlock);


        // Create In@R_61_2419@ @R_61_2419@ URI
        Uri in@R_61_2419@URI = Uri.parse("content://sms/in@R_61_2419@");

        // List required columns
        String[] reqCols = new String[] { "_id", "address", "body" };

        // Get Content Resolver object, which will deal with Content Provider
        ContentResolver cr = getContentResolver();

        // Fetch In@R_61_2419@ SMS Message from Built-in Content Provider
        Cursor c = cr.query(in@R_61_2419@URI, reqCols, null, null, null);

    }

Now, although this code is useless, just get the data and prepare the cursor so that I can traverse them. It will cause the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cryptail.stealthsms/com.cryptail.stealthsms.UnlockActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/in@R_61_2419@ from pid=4362, uid=10059 requires android.permission.READ_SMS, or grantUriPermission()

An error occurred on this line using cursor C = cr.query code and urged me to use read_ SMS permissions

This is my manifest XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cryptail.stealthsms" >


    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name=".UnlockActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:naame="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

You can see the include permissions. What might have caused this?

Edit 28.9.2015 – I didn't explain that I used the Android emulator in the Android studio, specifically Android 6.0 (API 23). This code is available under another analog device with different Android versions (4.4.2). So it may be an error of Android 6.0 or the simulator itself? Are there any changes to SMS permissions in a6.0?

resolvent:

So the problem is the new permission model in Android m mentioned by TDG

This article helps me understand this problem in a clearer way than official Android doc

Simple use

 if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {

Before executing any code related to SMS permission, if the permission does not exist, use the

final int REQUEST_CODE_ASK_PERMISSIONS = 123;
ActivityCompat.requestPermissions(UnlockActivity.this, new String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>