It seems this has to be done with a ContentResolver, but using information obtained in the answer to this question, I was able to implement one without resorting to having a database back-end. In App B (the one which App A relies on), I created the following Content Resolver:
public class ConfigProvider extends ContentProvider
{
public ConfigProvider() { }
@Override public int delete(Uri uri, String selection, String[] selectionArgs){ throw new UnsupportedOperationException("Not yet implemented"); }
@Override public String getType(Uri uri) { throw new UnsupportedOperationException("Not yet implemented"); }
@Override public Uri insert(Uri uri, ContentValues values) { throw new UnsupportedOperationException("Not yet implemented"); }
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { throw new UnsupportedOperationException("Not yet implemented"); }
@Override public boolean onCreate() { return false; }
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
//never mind the details of the query; we always just want to
//return the same set of data
return getConfig();
}
private Cursor getConfig()
{
//create a cursor from a predefined set of key/value pairs
MatrixCursor mc = new MatrixCursor(new String[] {"key","value"}, 1);
mc.addRow(new Object[] {"enabled", getEnabled()});
return mc;
}
private String getEnabled()
{
//access your shared preference or whatever else you're using here
}
}
Then make sure the ConntentProvider is registered in the manifest...
<provider
android:name=".ConfigProvider"
android:authorities="com.abcxyz.ConfigProvider" <!--[yourpackagename].ConfigProvider-->
android:enabled="true"
android:exported="true">
</provider>
And here's some sample code to access the setting from within App A:
Cursor c = getContentResolver().query(Uri.parse("content://com.abcxyz.ConfigProvider/anytype"), null, null, null, null);
HashMap<String, String> allValues = new HashMap<>();
while (c.moveToNext())
{
allValues.put(c.getString(0), c.getString(1));
}
if(allValues.containsKey("enabled"))
{
Toast.makeText(this, "enabled state: " + allValues.get("enabled"), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "value not found in cursor", Toast.LENGTH_LONG).show();
}