Content Resolver is null when called by second Application

Viewed 34

I am having an issues with Content Providers.

I am trying to retrieve data held in a SQLite database in Application 1 whilst in Application 2 but it is bringing back null. Where as when the exact same code is run in Application 1 it returns values.

I have also however given the correct permissions in the manifest for both Applications and the content provider itself is working fine on Application 1.

Application 1 Manifest

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <permission
        android:name="LOCATION_PROVIDER_READ_PERMISSION"
        android:protectionLevel="normal" />
    <permission
        android:name="LOCATION_PROVIDER_WRITE_PERMISSION"
        android:protectionLevel="normal" />

    <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.MyTheme"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/Theme.MyTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.MyTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan" />

        <provider
            android:name=".LocationContentProvider"
            android:authorities="com.example.application1.LocationContentProvider"
            android:enabled="true"
            android:exported="true"
            android:grantUriPermissions="true"
            android:multiprocess="true"
            android:readPermission="LOCATION_PROVIDER_READ_PERMISSION"
            android:writePermission="LOCATION_PROVIDER_WRITE_PERMISSION" />
    </application>


</manifest>

Application 2 Manifest

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="LOCATION_PROVIDER_WRITE_PERMISSION" />
    <uses-permission android:name="LOCATION_PROVIDER_READ_PERMISSION" />

    <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.MyTheme"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/Theme.MyTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.MyTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan" />
    </application>

</manifest>

Content Resolver used in Application 2, which looks at Application 1.

  try {

        String URL = "content://com.example.application1.LocationContentProvider/location_table";
        Uri uri = Uri.parse(URL);
        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(uri, null, null, null, null);

        if (c != null) {
            if (c.getCount() != 0) {
                c.moveToFirst();
                Toast.makeText(this, c.getString(c.getColumnIndex("ID")), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "its null", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Exception ex) {
        Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
    }

Can anyone suggest anything? I have honestly spent hours looking at everyone else's solutions, all of which i have tried and implemented into my own solution.

0 Answers
Related