我正在使用内容解析器读取android消息,日历数据等。我在清单文件中添加了权限。并实现了运行时权限以读取/编写SMS和日历。在运行android 7.0或更高版本的某些设备上,我仍然遇到问题。
Caused by: java.lang.SecurityException:
at android.os.Parcel.readException (Parcel.java:1693)
at android.os.Parcel.readException (Parcel.java:1646)
at android.app.ActivityManagerProxy.getContentProvider (ActivityManagerNative.java:4912)
at android.app.ActivityThread.acquireProvider (ActivityThread.java:6043)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider (ContextImpl.java:2474)
at android.content.ContentResolver.acquireUnstableProvider (ContentResolver.java:1521)
at android.content.ContentResolver.query (ContentResolver.java:520)
at android.content.ContentResolver.query (ContentResolver.java:478)
at com.allbackup.ui.activity.MsgsActivity.c (MsgsActivity.java:301)
at com.allbackup.ui.activity.MsgsActivity$c.a (MsgsActivity.java:201)
at com.allbackup.ui.activity.MsgsActivity$c.doInBackground (MsgsActivity.java:186)
上面的是读取SMS时错误的堆栈跟踪。下面是该代码:
Uri message = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
//startManagingCursor(c);
int totalSMS = c.getCount();
if (c.moveToFirst()) {
if(c.getString(c.getColumnIndex("address")) != null){
add = c.getString(c.getColumnIndexOrThrow("address")).replaceAll("[\\s\\-()]", "");
map.put("address", c.getString(c.getColumnIndexOrThrow("address")).replaceAll("[\\s\\-()]", ""));
}else{
map.put("address", "");
}
if(!add.isEmpty()){
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(add));
Cursor cno = getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME_PRIMARY, ContactsContract.Data.PHOTO_THUMBNAIL_URI},null,null,null);
if(cno.getCount()>0){
try {
cno.moveToFirst();
map.put("name", cno.getString(0));
map.put("photo", cno.getString(1));
} catch (Exception e) {
// TODO: handle exception
}finally{
// cno.close();
}
}else{
map.put("name", "");
map.put("photo", "");
}
if(cno!=null)
cno.close();
}
}
我在下一行出现错误:
Cursor cno = getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME_PRIMARY, ContactsContract.Data.PHOTO_THUMBNAIL_URI},null,null,null);
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.allbackup">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.activity.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
对于所有危险的权限,您需要在运行时请求权限。请参阅此link
答案 1 :(得分:0)
使用内容提供者访问内容之前,必须始终检查权限。如果未授予权限,则必须在运行时请求权限,然后尝试获取消息。
if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {
read your contacts here
}
有关更多信息,请访问此网站:https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en