权限拒绝:开放提供商

时间:2013-01-16 22:14:38

标签: android

我创建了一个自定义内容提供程序,将由更多应用程序访问。我在我的提供程序AndroidManifest.xml文件中包含了权限TAG,在第二个应用程序中,我包含了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 个答案:

答案 0 :(得分:18)

上面的答案让我感到困惑。但我现在明白了。我也想发布我的解决方案。也许对于某人来说,理解它会更好。

First App A是具有SQLite-Database和“Custom Content Provider”的应用程序。 App B使用ContentResolver来自App A的数据库。

这是来自App 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>

好的,这是来自App 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;
}
}

App 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();
}
}

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

答案 1 :(得分: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>

首先,你真的真的应该在这些权限名称上加上命名空间。让他们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值需要使用android:name中的<permission>值,而不是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:name,也不会使用android: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

进行这些更改,我认为你会有更好的运气。

答案 2 :(得分:1)

我遇到了同样的问题,所以我记得我忘记了

  

在清单中为提供商添加导出标签

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

要访问另一个应用程序,您需要导出true,这允许其他应用程序从此主机应用程序访问Content Provider。

答案 3 :(得分:0)

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

相关问题