我正在尝试从设备上读取短信,但我遇到了以下错误:
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()
我已将权限包含在我的Manifest文件中,并尝试通过以下代码访问邮件:
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>
从错误中看,许可似乎没有因某种原因进行注册。所以我试着看看系统是否通过以下代码接受了许可:
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");
}
输出是else语句,NOT GRANTED。
非常感谢帮助!