android - 权限拒绝 : opening provider

标签 android

我创建了一个自定义内容提供程序,将由更多应用程序访问。我在提供程序 AndroidManifest.xml 文件中包含了权限标签,在第二个应用程序中,我包含了使用权限标签,但没有成功。 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>

最佳答案

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

第一个应用程序 A 是具有 SQLite 数据库和“自定义内容提供程序”的应用程序。应用 B 通过 ContentResolver 使用来自应用 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 文件。重要的是带有“使用权限”的部分:

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

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

关于android - 权限拒绝 : opening provider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14368867/

相关文章:

android - 如何使用 Jetpack Compose 将 BottomSheetScaffold 扩展到特定高度?

android - 如何解决 Android TV 模拟器 DNS 查找错误? (java.net.UnknownHostException : Unable to resolve host)

机器人 : open chrome custom tab from fragment

java - 在位于 onViewCreated() 外部的 Runnable 内部使用 Glide

android - 如何关闭并重新打开 Sqlite 数据库?

java - Android - 自定义 ImageView,带有勾选标记图像为选定状态

android - RxJava2 如何将 Single 链接到 Completable,以便在 Completable 完成时订阅它

java - 将数据发送到 php 脚本并通过 android 保存在 mysql 表中时,加号变为空格

android - 如何在 Android 应用程序中显示动画 GIF 图像?

android - 如何在android中以编程方式添加/删除电话号码以自动拒绝列表