0

I want to read SMS using a content resolver. I've added permissions in the manifest file like this:

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

Code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Uri uriSMSURI = Uri.parse("content://sms/inbox");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null); //line that gives exception
    cur.close();}

But it's giving an error:

02-22 02:55:36.429 18963-18963/inc.osi.imossis E/AndroidRuntime: FATAL EXCEPTION: main
 Process: inc.osi.imossis, PID: 18963
 java.lang.RuntimeException: Unable to start activity ComponentInfo{inc.osi.imossis/inc.osi.imossis.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=18963, uid=10059 requires android.permission.READ_SMS, or grantUriPermission()
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=18963, uid=10059 requires android.permission.READ_SMS, or grantUriPermission()
     at android.os.Parcel.readException(Parcel.java:1599)
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
     at android.content.ContentResolver.query(ContentResolver.java:491)
     at android.content.ContentResolver.query(ContentResolver.java:434)
     at inc.osi.imossis.MainActivity.onCreate(MainActivity.java:30)
     at android.app.Activity.performCreate(Activity.java:6237)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

The complete manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="inc.osi.imossis">
    <uses-permission android:name="android.permission.READ_SMS"/>
    <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>
</manifest>

How do I solve it?

5
  • Show your entire manifest, please. Feb 21, 2016 at 22:21
  • @Doug Done please check Feb 21, 2016 at 22:28
  • What is your targetSdkVersion? What version of Android are you testing on? Feb 21, 2016 at 22:32
  • @CommonsWare API 19 kitkat Feb 21, 2016 at 23:50
  • @Irtza Shahan: try my suggestion n please let me know if it worked for you or not?
    – srv_sud
    Feb 22, 2016 at 17:47

3 Answers 3

0

Have you tried cleaning and re-building the project?

edit: Can you try checking and requesting the permission at run-time?

final private int REQUEST_READ_SMS_ID = 1;  // A constant needed for callback
int hasReadSMSPermission = checkSelfPermission(Manifest.permission.READ_SMS);
if (hasReadSMSPermission != PackageManager.PERMISSION_GRANTED) {
    requestPermissions(new String[] {Manifest.permission.hasReadSMSPermission }, REQUEST_CODE_ASK_PERMISSIONS);
    return;
}`
1
  • Tried everything in my brain could think off but to no avail Feb 22, 2016 at 11:52
0

Uninstall the app and install again and try. As manifest changes does not reflect on re-installations sometimes.

If I am not wrong, may be you have modifies the manifest file after installation. Just try your luck, it may work.

1
  • Did not there was a bug in emulator of API level 23 in android studio Feb 24, 2016 at 21:39
0

So I found the problem after 2 days of Headbanging search for solution there is a bug in Emulator of API 23 if Any one Else faces this problem just run it on a different device or on emulator of api level 18 or 19 it'll work. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.