Skip to content

Share your ContentProviders across apps

rutura edited this page Apr 17, 2017 · 2 revisions
  • This example contains two Apps: ProviderApp contains a ContentProvider subclass that exposes methods to maniputlate the settings.ConsumerApp uses these interfaces to modify the settings in the ProviderApp

  • ProviderApp exposes the ContentProvider as normal:

  • It defines permissions and content-filters appropriately:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blikoon.app5100provider">

    <permission
        android:name="com.blikoon.app5100provider.permission.READ_PREFERENCES"
        android:label="Read Application Settings"
        android:protectionLevel="normal"/>
    <permission
        android:name="com.blikoon.app5100provider.permission.WRITE_PREFERENCES"
        android:label="Write Application Settings"
        android:protectionLevel="dangerous"/>

    <uses-permission android:name="com.blikoon.app5100provider.permission.READ_PREFERENCES"/>
    <uses-permission android:name="com.blikoon.app5100provider.permission.WRITE_PREFERENCES"/>


    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.blikoon.app5100provider.ACTION_SETTINGS"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <provider
            android:name=".SettingsProvider"
            android:authorities="com.blikoon.app5100provider.settingsprovider"
            android:readPermission="com.blikoon.app5100provider.permission.READ_PREFERENCES"
            android:writePermission="com.blikoon.app5100provider.permission.WRITE_PREFERENCES"
            android:exported="true"
            >
        </provider>
    </application>

</manifest>
  • Take care to notice the intent-filters and how the permissions are specified.
  • The ContentProvider subclass supplies the necessary overrides and implementations.
  • ConsumerApp does nothing special, it just uses ContentResolvers as it would use any other.The android system takes care of enforcing the permissions we specified.ConsumerApp updates the provider app with some data:
    ContentValues cv = new ContentValues(2);
        cv.put(SettingsColumns.NAME, "preferenceEnabled");
        cv.put(SettingsColumns.VALUE, isChecked);

        //Update the provider, which will trigger our observer
        getContentResolver().update(SETTINGS_CONTENT_URI, cv, null, null);
  • Your Provider declared in the manifest has to be flaged exported :
            android:exported="true"

otherwise external apps on some versions of android won't be able to access your settings. See http://stackoverflow.com/questions/18847302/securityexception-permission-denial-opening-provider

Clone this wiki locally