Note
Note

Reputation: 183

Cannot access custom content provider from another app

I have 2 app and i want app A share data with B by content provider, i can access content provider in app A but not in app B.I feel like my permission mess up but idk how to fixe it. Heres my file... App A's manifest file:

 <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>

and B's manifest file:

<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>

Custom Content Provider:

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
}

and every time i call this in app B

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

cursor always null. Ive tried all the solutions that i found but nothing work.Help :"<

Upvotes: 3

Views: 1490

Answers (1)

Note
Note

Reputation: 183

I realize that im using android 11 and they limit the package visibility so just need to add

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

in app B's manifest (not in application tag) file so B can see your custom content provider of A

Upvotes: 10

Related Questions