访问另一个应用程序的内容提供者时拒绝权限

时间:2017-03-27 17:51:20

标签: android android-security android-securityexception

我用于理解内容提供商的教程

  

AppA:https://www.tutorialspoint.com/android/android_content_providers.htm

我创建了另一个我打算使用的应用程序来访问教程应用程序的Content Provider

  

AppB清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.MyApplication2">
    <uses-permission android:name="com.example.MyApplication.StudentProvider.READ_PERMISSION"/>
    <uses-permission android:name="com.example.MyApplication.StudentProvider.WRITE_PERMISSION"/>
    <uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
    <uses-permission android:name="android.permission.permRead"/>


    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
  

AppB部分代码:

String URL = "content://com.example.MyApplication.StudentsProvider/students";

Uri uri = Uri.parse(URL);

Cursor c = getContentResolver().query(uri,null,null,null,null);


String _ID = "_id";
String NAME = "name";
String GRADE = "grade";

if (c.moveToFirst()) {
    do{
        Toast.makeText(this,
                c.getString(c.getColumnIndex(_ID)) +
                        ", " +  c.getString(c.getColumnIndex(NAME)) +
                        ", " + c.getString(c.getColumnIndex(GRADE)),
                Toast.LENGTH_SHORT).show();
    } while (c.moveToNext());
}

我收到以下错误:

  

Caused by: java.lang.SecurityException: Permission Denial: opening provider

为什么我收到此错误的任何想法?任何帮助表示赞赏!

编辑:我得到了解决方案! ( //此行是新的 &amp; //行修改

String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
ContentResolver cr = getContentResolver(); //This line is new
Cursor c = cr.query(uri,null,null,null,null); //Line modified

String _ID = "_id";
String NAME = "name";
String GRADE = "grade";

if (c.moveToFirst()) {
do{
    Toast.makeText(this,
            c.getString(c.getColumnIndex(_ID)) +
                    ", " +  c.getString(c.getColumnIndex(NAME)) +
                    ", " + c.getString(c.getColumnIndex(GRADE)),
            Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}

现在我的AppB能够从AppA读取ContentProvider,但是我仍然不明白为什么这会有所作为!

EDIT2:已经用ChangedManifest XML替换了教程清单

2 个答案:

答案 0 :(得分:0)

我在你的AndroidManifest.xml(AppA)中没有看到这个:

  <provider android:name="StudentsProvider"
     android:authorities="com.example.MyApplication.StudentsProvider"
     android:grantUriPermission="true" 
  />

如果这没有帮助,那么它可能是运行时权限:

https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

解决方案:在声明/请求/授予所有必要权限后,需要使用内容解析程序访问其他应用程序内容提供程序。