Yeah exactly, I was wondering if I was missing a way to pass data
synchronously using intents, similar in a sense to the way activities
can do it with startActivityForResult(). I haven't looked at the aidl
remote service stuff in about a year, and forgot whether that lets the
processes communicate synchronously, but it seems like a good deal
more code to add to support it. There is also the content provider as
you mentioned, I'm not sure if that allows synchronous access.

I implemented a simple asynchronous scheme for now as follows:

  class ThirdPartyService extends Service {
      void doWork() {
          Intent intent = new Intent();
          intent.setClassName("myapp", "myapp.MyService");
          intent.putExtra("callbackpackage", "thirdpartyapp");
          intent.putExtra("callbackclassname", "ThirdPartyReceiver");
          startService(intent);
      }
  }

  class ThirdPartyReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          .. do something with the data sent back from my app ..
      }
  }

  class MyService extends IntentService {
      @Override
      public void onHandleIntent(Intent intent) {
          Intent i = new Intent();
          i.setClassName(
              intent.getStringExtra("callbackpackage"),
              intent.getStringExtra("callbackclassname"));
          i.putExtra("somedata", "hi there");
          sendBroadcast(i);
      }
  }

So the third party sends an intent which gets picked up by MyService.
They supply their package and classname as extras in the intent. After
my service is done working, I can broadcast a result intent back to
them (I could send it via startActivity() too). They have to mark
their receiver as exported=true. Then they can do whatever they want
with the data in the intent. It works, but it's not as convenient (for
the third party) as exposing some asynchronous call mechanism. Then
they will get the data in the calling context.

I'll check out the content provider and the aidl interfaces to see if
those would be more convenient,

Thanks


On Sep 17, 12:35 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
>   Mark,
>
> Your subject says "synchronous" - afaik, only content providers are
> synchronous. Service startup / binding is asynchronous, binder requires
> a service (although I could be wrong on this one), and so is
> asynchronous as well. Intents are of course asynchronous too.
>
> A content provider doesn't have to be backed by SQL, or to really
> implement data storage - you could pass various commands encoded in the
> URI, returning results in your own Cursor implementation.
>
> If you are keen on using intents, though - your code is pretty close,
> but you need to use startActivityForResult rather than startActivity, so
> that you can retrieve the result (set with setResult) later. This can't
> be used from a Service, only from an Activity, though.
>
> -- Kostya
>
> 17.09.2010 20:02, Mark Wyszomierski пишет:
>
>
>
>
>
> > Hi,
>
> > I've got an application, and I'd like to publish a way for third-party
> > services to get data from it. Ideally I could expose this  through
> > intents, as in the following pseudocode:
>
> >     // third party apps
> >     class ThirdPartyService extends Service {
> >         @Override
> >         public void onCreate() {
> >             Intent i = new Intent(this, "com.me.test.MyAppIntent");
> >             Intent result = startActivity(i);
> >         }
> >      }
>
> >      // part of my app
> >      class MyAppIntent extends Activity {
> >         @Override
> >         public void onCreate() {
> >             Intent i = new Intent();
> >             i.putExtra("result", "hi there");
> >             setResult(i);
> >             finish();
> >         }
> >      }
>
> > The above won't work of course - I have some work done on this, just
> > wondering if anyone has any recommendations or best practices?
>
> > Thanks
>
> --
> Kostya Vasilyev -- WiFi Manager + pretty widget 
> --http://kmansoft.wordpress.com

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to