Hi.
I need third party applications ("Foo") to get information from my application ("Bar"), but my solution so far seems cumbersome:
- Application Foo needs information from Bar and sends a broadcast ("bar.POLL").
- Application Bar listens for this broadcast, and replies with another broadcast ("bar.PUSH");
- Foo listens for bar.PUSH and reads the contents of the included Bundle.
Is there a more direct way to do this?
EDIT: I solved it with an extremely simplistic ContentProvider as Guido suggested:
public class MyProvider extends ContentProvider {
private String state = "";
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
MatrixCursor cursor = new MatrixCursor(new String[]{"state"});
cursor.addRow(new Object[]{state});
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
state = (String) values.get("state");
return 1;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
}
Remember to add the provider to the manifest:
<provider android:name=".MyProvider" android:authorities="com.example.hello" />
Update the state from an Activity like this:
ContentValues cv = new ContentValues();
cv.put("state", "myNewState");
getContext().getContentResolver().update(Uri.parse("content://com.example.hello"), cv, null, null);
Get content from the provider in the external app:
Cursor cur = managedQuery(Uri.parse("content://com.example.hello"), null, null, null, null);
if (cur.moveToFirst()) {
String myContent = cur.getString(0);
}