权限拒绝:打开提供程序

25

我创建了一个自定义内容提供程序,将被其他几个应用程序访问。我已经在我的提供程序AndroidManifest.xml文件中包含了permission标签,并在第二个应用程序中包含了uses-permissions标签,但没有成功。 Logcat向我显示:

java.lang.SecurityException: Permission Denial: opening provider com.company.contentprovider.AplicacaoContentProvider requires READ_DATABASE or WRITE_DATABASE.

我在寻找类似问题的答案,但似乎一切都是正确的。有什么想法吗? 谢谢!!!

这是我的提供者AndroidManifest.xml文件:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<permission android:name="READ_DATABASE" android:label="@string/app_read"       android:protectionLevel="normal"></permission>
<permission android:name="WRITE_DATABASE" android:label="@string/app_write" android:protectionLevel="normal"></permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".CompanyProvider"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider android:name="AplicacaoContentProvider"
        android:authorities="com.company.contentprovider"
        android:exported="true"
        android:readPermission="@string/app_read"
        android:writePermission="@string/app_write"
       />
</application>

这是我的第二个应用程序的AndroidManifest.xml文件:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permissions.READ_DATABASE"/>
<uses-permission android:name="android.permissioms.WRITE_DATABASE"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testeprovider.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>

4个回答

25

上面的答案对我来说有点困惑。但现在我明白了。我也想发布我的解决方案。也许对某些人来说更容易理解。

首先,应用程序A是具有SQLite数据库和“自定义内容提供程序”的应用程序。应用程序B使用ContentResolver从应用程序A中的数据库。

这是应用程序A的AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />


<permission android:name="de.test.READ_DATABASE" android:protectionLevel="normal" />
<permission android:name="de.test.WRITE_DATABASE" android:protectionLevel="normal" />

<application
    android:debuggable="true"
    ... >
    ...
    ...
    <provider
        android:name="de.test.TestContentProvider"
        android:authorities="de.test.ContentProvider"
        android:exported="true"
        android:readPermission="de.test.READ_DATABASE"
        android:writePermission="de.test.WRITE_DATABASE" />
    ...
    ...
</application>

好的,这是应用程序B的AndroidManifest.xml文件。重要的是带有“uses-permission”的部分:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.test.testercontentprovider"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-permission android:name="de.test.READ_DATABASE" />
<uses-permission android:name="de.test.WRITE_DATABASE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="de.test.testercontentprovider.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>

App A 的 ContentProvider 代码如下:

public class TestContentProvider extends ContentProvider {

public static final String AUTHORITY = "de.test.TestContentProvider";

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + "nameoftable");


@Override
public boolean onCreate() {
    ...
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    // TODO Auto-generated method stub
            return null;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
}
}

而来自应用程序 B 的 ContentResolver 代码:

public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getSimpleName();
public static final String AUTHORITY = "de.test.TestContentProvider";
public static final String TABLE_NAME = "nameoftable";

    ...

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

    ContentResolver cr = getContentResolver();

    // show entries of db
    listEntries(cr);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void listEntries(ContentResolver cr) {
    Uri uri = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
    Cursor c = cr.query(uri, null, null, null, null);

    if (c == null) {
        Log.d(TAG, "Cursor c == null.");
        return;
    }
    while (c.moveToNext()) {
        String column1 = c.getString(0);
        String column2 = c.getString(1);
        String column3 = c.getString(2);

        Log.d(TAG, "column1=" + column1 + " column2=" + column2 + " column3=" + column3);
    }
    c.close();
}
}

我希望这可以帮助某人更好地理解它。


19

但似乎并不完全正确

并非完全如此。

<permission android:name="READ_DATABASE" android:label="@string/app_read"       android:protectionLevel="normal"></permission>
<permission android:name="WRITE_DATABASE" android:label="@string/app_write" android:protectionLevel="normal"></permission>

首先,你真的真的真的真的真的应该在这些权限名称上加上命名空间。 将它们命名为com.company.contentprovider.READ_DATABASEcom.company.contentprovider.WRITE_DATABASE

<provider android:name="AplicacaoContentProvider"
    android:authorities="com.company.contentprovider"
    android:exported="true"
    android:readPermission="@string/app_read"
    android:writePermission="@string/app_write"
   />

其次,您的android:readPermissionandroid:writePermission值需要使用<permission>中的android:name值,而不是android:labelandroid:label仅用于显示名称。因此,上面的代码应该是:

<provider android:name="AplicacaoContentProvider"
    android:authorities="com.company.contentprovider"
    android:exported="true"
    android:readPermission="com.company.contentprovider.READ_DATABASE"
    android:writePermission="com.company.contentprovider.WRITE_DATABASE"
   />

(虽然这样做会获得额外的奖励分,但是明确地添加android:exported="true"是一个好主意)

<uses-permission android:name="android.permissions.READ_DATABASE"/>
<uses-permission android:name="android.permissioms.WRITE_DATABASE"/>

第三,您的另一个清单文件没有使用您旧的android:name,也没有使用我建议修改后的android:nameandroid:label,而是使用了完全不同的东西,您选择说这些在android.permission命名空间中,但实际上它们不在该命名空间中。应该这样写:

<uses-permission android:name="com.company.contentprovider.READ_DATABASE"/>
<uses-permission android:name="com.company.contentprovider.WRITE_DATABASE"/>

(虽然com.company.contentprovider.WRITE_DATABASE可能已足够--我不知道android:writePermission是否会自动暗示android:readPermission

做出这些更改,我相信你将会有更好的运气。


1
@MauricioAlencar:“我想,可以在每个应用程序的AndroidManifest.xml文件中包含权限和uses-permission标记之类的东西。这样会有效吗?”--这一部分是可行的。事实上,在“插件”类型的情况下,这通常是一个好的做法。 - CommonsWare
1
我遇到了一个奇怪的问题。如果我先安装接收器应用程序,再安装内容提供者应用程序,就会出现java.lang.SecurityException: Permission Denial: opening provider。但是如果我反过来安装,就可以正常工作。 - Nishant Shah
1
android:exported = "true" 是解决方案。 - Abhishek Dhote
@AbhishekDhote 这对我有用。真的很奇怪,因为文档说内容提供者的默认值是 exported=true,但是一旦我在主机应用程序的清单中显式设置了这个值,权限拒绝错误就消失了。 - Cody
1
@Cody:true 已经不再是 android:exported 的默认值了。对于 android:minSdkVersionandroid:targetSdkVersion 设置为 16 或更低版本的应用程序,默认情况下提供程序是公开的。对于所有其他应用程序,默认情况下提供程序不会被公开。换句话说,这很混乱。 - CommonsWare
显示剩余4条评论

4

我之前遇到了同样的问题,后来想起来自己忘记在清单文件中为提供程序添加导出标签了。

要在清单文件中为提供程序添加导出标签。

   <provider
        android:name=".UsersProvider"
        android:authorities="com.xyz.demoapplication"
        android:exported="true">
   </provider>

为了在另一个应用程序中访问,您需要导出。这将允许其他应用程序从此主机应用程序访问内容提供程序。

0
我遇到了同样的问题。请确保您在AndroidManifest中声明的权限和URI.parse(URL)中的URL是相同的。这可能是问题所在。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接