[android-developers] Main Activity/Service Communication

2013-01-19 Thread dashman
This topic has been hashed quite a bit - but there doesn't seem
to be a clear answer - at least not one that i could find.

Background - I'm developing an app and I think some processing can
be done in the background - e.g. database and image processing
(create bitmaps, rotate etc - i can do that in a background right?).

So I'd like an efficient way of calling the background when to start
a specific job and then have the background service notify the caller
with a result when done.

Option 1 - create a service - that waits and returns status. haven't
figured out a way of doing the latter.

Option 2 - I suppose I could start a new activity with no UI and then call
startIntentForResult(). i.e. an Acitivy for each background job.


Suggestions?


-- 
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

Re: [android-developers] Main Activity/Service Communication

2013-01-19 Thread Harri Smått
Hi,

You could also consider option 3;

Start an asynchronous task (native Java Thread or AsyncTask) for your 
processing needs.

--
H

On Jan 19, 2013, at 3:15 PM, dashman  wrote:

> Option 1 - create a service - that waits and returns status. haven't
> figured out a way of doing the latter.
> 
> Option 2 - I suppose I could start a new activity with no UI and then call
> startIntentForResult(). i.e. an Acitivy for each background job.

-- 
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


Re: [android-developers] Main Activity/Service Communication

2013-01-19 Thread TreKing
On Sat, Jan 19, 2013 at 7:15 AM, dashman  wrote:

> Suggestions?


Have your Service send a Broadcast when it's done, then Receive it
appropriately.

-
TreKing  - Chicago
transit tracking app for Android-powered devices

-- 
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

Re: [android-developers] Main Activity/Service Communication

2013-01-19 Thread dashman
I'm concerned about the overhead with this mechanism.

Also not too sure how to notify the service of a new job.

If the foreground activity and service share an object - i assume i can
use standard java synchronization - i.e. wait() and notifyall() etc.


On Saturday, January 19, 2013 2:03:24 PM UTC-5, TreKing wrote:
>
> On Sat, Jan 19, 2013 at 7:15 AM, dashman  >wrote:
>
>> Suggestions?
>
>
> Have your Service send a Broadcast when it's done, then Receive it 
> appropriately.
>
>
> -
> TreKing  - Chicago 
> transit tracking app for Android-powered devices
>  

-- 
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

Re: [android-developers] Main Activity/Service Communication

2013-01-19 Thread Kristopher Micinski
You don't want to do that.  The traditional Java types of
synchronization methods don't really work well here because your
process can die, and you shouldn't be passing references across
activities anyway.  Before being too concerned about the overhead of
the mechanism you should profile it. Millions of apps do messaging by
broadcasts anyway.

For doing this kind of thing, most people use something like an
IntentService which accepts its Intent input as the actual "new job"
and allows the service to notify the activity of completion via a
Messenger/Handler.  For jobs that persist across activities you might
consider using a broadcast.  But most of the time, you could simply
save the completed information in a database, and then have a content
observer to watch for changes.

(You should be serializing as much state as possible in a database
anyway, since your application can die at pretty much any point and
should be able to restart..  What happens with the low memory killer
kills your app when a new job has been issued?  You can also serialize
jobs in the database in case this happens.)

kris

On Sat, Jan 19, 2013 at 9:22 PM, dashman  wrote:
> I'm concerned about the overhead with this mechanism.
>
> Also not too sure how to notify the service of a new job.
>
> If the foreground activity and service share an object - i assume i can
> use standard java synchronization - i.e. wait() and notifyall() etc.
>
>
> On Saturday, January 19, 2013 2:03:24 PM UTC-5, TreKing wrote:
>>
>> On Sat, Jan 19, 2013 at 7:15 AM, dashman  wrote:
>>>
>>> Suggestions?
>>
>>
>> Have your Service send a Broadcast when it's done, then Receive it
>> appropriately.
>>
>>
>> -
>> TreKing - Chicago transit tracking app for Android-powered devices
>
> --
> 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

-- 
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


Re: [android-developers] Main Activity/Service Communication

2013-01-20 Thread TreKing
On Sat, Jan 19, 2013 at 8:22 PM, dashman  wrote:

> I'm concerned about the overhead with this mechanism.
>

Why?


> Also not too sure how to notify the service of a new job.
>

IntentService, as Kris mentioned, can just be called repeatedly and it will
queue the messages up and handled them one by one.


> If the foreground activity and service share an object - i assume i can
> use standard java synchronization - i.e. wait() and notifyall() etc.
>

You shouldn't have to do that. What object would they be sharing? If
they're running at the same time, then you probably want to bind to the
Service which allows you to communicate with the Service directly.

-
TreKing  - Chicago
transit tracking app for Android-powered devices

-- 
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