views:

52

answers:

1

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);
}
+2  A: 

You should expose a ContentProvider.

"Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access."

Content providers implement a common interface for querying the provider and returning results. It is not hard to implement, but maybe the official documentation is not the best to get started with it. You can find other examples at:

Guido
You could also try using a service. It depends what you want to store/send
Falmarri
Falmarri: I'd still need some form of RPC method, right? I already use broadcasts within a Service.
neu242
If you use a ContentProvider, you can query the data you need by asking a ContentResolver for a given URI. I don't understand why you would need RPC-stuff here. A content provide is the natural way to do it, IMO.
Guido
Hmm. A ContentProvider seems a bit overkill for the task at hand. "Foo" only needs to know the current value of a boolean flag in "Bar"...
neu242
Then it could be a bit overkill :) This post explains how to use SharedPreferences to share data between applications. I don't know if that is too triky or if it even works : "Getting SharedPreferences from other application in Android" http://thedevelopersinfo.com/2009/11/25/getting-sharedpreferences-from-other-application-in-android/
Guido