【问题标题】:Cannot access custom content provider from another app无法从其他应用访问自定义内容提供程序
【发布时间】:2021-07-15 17:03:18
【问题描述】:

我有 2 个应用程序,我希望应用程序 A 通过内容提供商与 B 共享数据,我可以在应用程序 A 中访问内容提供商,但不能在应用程序 B 中访问。我觉得我的权限搞砸了,但我不知道如何修复它。 这是我的文件... 应用 A 的清单文件:

 <permission android:name="myper.READ" android:protectionLevel="normal"  />
<permission android:name="myper.WRITE" android:protectionLevel="normal"  />

<application
    android:name=".ui.MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Ex">

    <activity android:name="com.abcd.aaaaa.ui.MainActivity"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:authorities="com.advance.usercontentprovider"
        android:name=".contentprovider.UserContentProvider"
        android:enabled="true"
        android:exported="true"
        android:multiprocess="true"
        android:readPermission="myper.READ"
        android:writePermission="myper.WRITE"
    />
</application>

和B的清单文件:

<uses-permission android:name="myper.READ" />
<uses-permission android:name="myper.WRITE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.aaaa">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

自定义内容提供者:

val PROVIDER_NAME = "com.advance.usercontentprovider"

val URL = "content://$PROVIDER_NAME/users"

val CONTENT_URI = Uri.parse(URL)

override fun onCreate(): Boolean {
    return true
}

val URI_ALL_ITEMS_CODE = 1
val URI_ONE_ITEM_CODE = 2
var uriMatcher: UriMatcher? = null
init
{
    uriMatcher = UriMatcher(UriMatcher.NO_MATCH)
    uriMatcher!!.addURI(PROVIDER_NAME, "users", URI_ALL_ITEMS_CODE)
    uriMatcher!!.addURI(PROVIDER_NAME, "users" + "/#", URI_ONE_ITEM_CODE)
}


override fun query(
    uri: Uri,
    projection: Array<out String>?,
    selection: String?,
    selectionArgs: Array<out String>?,
    sortOrder: String?
): Cursor? {
    /// do smt
}

override fun getType(uri: Uri): String? {
    // do smt
}

override fun insert(uri: Uri, values: ContentValues?): Uri? {
    // do smt
}

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
    // do smt
}

override fun update(
    uri: Uri,
    values: ContentValues?,
    selection: String?,
    selectionArgs: Array<out String>?
): Int {
    // do smt
}

每次我在应用程序 B 中调用它

 val cursor = contentResolver?.query(
            Uri.parse("content://com.advance.usercontentprovider/users")
            , null, null, null, null)

光标始终为空。 我已经尝试了我找到的所有解决方案,但没有任何效果。帮助:"

【问题讨论】:

  • /// do smt 如果你不做任何事情,你不认为你的光标是空的吗?
  • Custom Content Provider: ?我们甚至看不到类名。
  • android:name=".contentprovider.UserContentProvider 这将取决于您的应用程序包名称。试试 android:name=".UserContentProvider
  • 我的意思是 query() 甚至没有被调用所以我把 'do smt' 放在那里

标签: android kotlin android-contentprovider


【解决方案1】:

我意识到我使用的是 android 11,它们限制了包的可见性,所以只需要添加

<queries>
    <provider
        android:authorities="com.advance.usercontentprovider" />
</queries>

在应用程序 B 的清单(不在应用程序标签中)文件中,以便 B 可以看到 A 的自定义内容提供程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多