0

I'm attempting to read the sms content, but failing with a PermissionDenial exception. I believe I have the syntax correct, but may possibly be missing necessary elements from my manifest other than the permission (I created it uses Android Studio's New Project wizard).

My AndroidManifest.xml file is (Note that I HAVE moved the uses-permission above and below the application in the xml):

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.READ_SMS" />

</manifest>

and the function that is reading the sms messages is:

public void loadMessages(){
    Uri allMessages = Telephony.Sms.CONTENT_URI;
    Cursor cursor = this.getContentResolver().query(allMessages, null, null, null, null);
    while (cursor.moveToNext()){
        for (int i = 0; i < cursor.getColumnCount(); i++){
            Log.d(cursor.getColumnName(i) + "", cursor.getString(i) + "");
        }
        Log.d("one row finished", "*********");
    }
}

1 Answers1

0

The uses-permission tag must be above your application tag. Not sure why -- most likely just an artifact of how manifest parsing is done on the device.

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
  • Yeah, I've tried both ways with no luck. It still throws the same exception. – user3442536 Oct 25 '15 at 23:33
  • 1
    Are you running on Marshmallow? The READ_SMS permission is marked as "dangerous", meaning you have to use the new APIs to *actually* be granted the permission. – Snild Dolkow Oct 25 '15 at 23:42