0

I'm attempting to read the SMS messages off of my device, but I'm running into the following error:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{package.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=11441, uid=10058 requires android.permission.READ_SMS, or grantUriPermission()

I've included the permission in my Manifest file, and am trying to access the messages through the following code:

Uri inboxURI = Uri.parse("content://sms/inbox");

Cursor cur = getContentResolver().query(inboxURI, null, null, null, null);

while (cur.moveToNext()) {
    String body = cur.getString(cur.getColumnIndexOrThrow("body"));
    Log.i("ADS", body);
}
cur.close();

The Manifest:

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

    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15"></uses-sdk>

    <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=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>


</manifest>

From the error, it seems that the permission isn't registering for some reason. So I tried to see if the system was accepting the permission through the following code:

String permission = "android.permission.READ_SMS";
int res = getApplicationContext().checkCallingOrSelfPermission(permission);
if (res == PackageManager.PERMISSION_GRANTED) {
    Log.i("Permission Given?", "GRANTED");
} else {
    Log.i("Perimssion Given?", "NOT GRANTED");
} 

And output was the else statement, NOT GRANTED.

Help would be greatly appreciated!

benzabill
  • 259
  • 1
  • 8
  • 21
  • what is this:- (android:minSdkVersion="15" android:targetSdkVersion="15") both are same – Androider Nov 10 '15 at 18:17
  • I added them as part of my process to figure out why the Manifest I have isn't working - I don't have them set at 15 for any particular reason. Removing them/Having them in don't affect anything – benzabill Nov 10 '15 at 18:22
  • Are you using Android Studio / Gradle? If so, your `build.gradle` overrides any `minSdkVersion`/`targetSdkVersion` you have set in your manifest - what values are set in your `build.gradle`? – ianhanniballake Nov 10 '15 at 18:23
  • Ah, I see: compileSdkVersion 23...buildToolsVersion "23.0.1"... minSdkVersion 15...targetSdkVersion 23 – benzabill Nov 10 '15 at 18:24
  • I've never worked with the gradle aspect of android development - I used to use eclipse. Now I use Android Studio, do i need to do something to make my Manifest changes affect the gradle build? (I've built and cleaned my code several times) – benzabill Nov 10 '15 at 18:25
  • As I said, your `minSdkVersion` and `targetSdkVersion` are ignored in your `AndroidManifest.xml` - just update your `build.gradle` files with what values you want – ianhanniballake Nov 10 '15 at 18:33
  • 1
    Thanks Ian for pointing me towards the build.gradle file, I hadn't considered that part – benzabill Nov 10 '15 at 18:39

0 Answers0