• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Work with binded service

QuickNick

Newbie
Good day, friends.

I'm wanting to work with binded service: in my (attached) sample DigitService generates 3 numbers, sends them by DigitBinder and DigitActivity should redraw itself - show these 3 digits.

Unusually, there is no such example in ApiDemos - where service transacts data through Binder and Activity catches it. (Solution with IInterface and .aidl would be left for later, not today).

So - how can I transact data in my example? What classes and methods should I add?

View attachment SampleApplicationA.zip
 
I havent looked at the zip it sounds like you just need a callback. Go easy in the beginning too aidl is for inter-PROCESS communication. This is VERY rare with Android apps. A single process can run several apps at the same time.

All you need to marshall data between a service and activity is a reference or bundle. Binder is just a fancy reference to help handle callbacks.

The LocalBinder provides the getService() method for clients to retrieve the current instance of LocalService. This allows clients to call public methods in the service. For example, clients can call getRandomNumber() from the service.
From: Bound Services | Android Developers


This might help too:
Google I/O 2010 - Android REST client applications - YouTube
 
This allows clients to call public methods in the service. For example, clients can call getRandomNumber() from the service.

Yes, this solution is obvious. But in my example not the activities should ask service about results, but service should notify activities about result.

I found a more simple structure for this purpose, but it's still interesting how to do it with service.

P.S.: my solution: construct the model as singleton. Constuct the interface with method "void publishResults(Object args)", all client activities should implement this interface. Model contains list of these interfaces - and when it async-task publishes progress, model notifies every interface in the list. We should also have option to register interface in list (to add it) or unregister it (to remove it).

Maybe, Android SDK has similar instruments, but I'm newbie :)
 
Sounds like what you have will work, you should be careful with singletons though.


Another similar way to do this is like this:
(Almost the same as what you are doing except using the Service binder instead of a singleton)

(Service class)
Code:
ArrayList observeringActivities = new ArrayList<IObservable>();

public void registerObserver(IObservable observer)
{
    observeringActivities.add( observer );
}
Then have your activity implement IObserver

Code:
public interface IObserver

void notify();
Example activity

Code:
public class MyActivity extends Activity implements IObserver
{
    Service myService; 

    public void onCreate( Bundle savedInstanceState )
    {
         //setcontentview, bind the service etc

         myService = myBinder.getService();
         myService.registerObserver(this);
    }

    public void onDestroy()
    {
         myService.unregisterObserver(this);  //remove from ArrayList
    }

    //you could also do this in onStart()/stop, onPause()/resume
}
finally when something happens you could run this code in the service:
Code:
for(IObserver observer : observeringActivities)
{
    observer.notify();
}
The only thing here is you need to be careful and make sure to unregister, otherwise the service will hold a reference to the activity and the garbage collector will not be able to collect it even when it's long gone (assuming you are using multiple activities)


There are also probably 5 other ways to do this, and I am no expert but thought it worth mentioning this way too :)
 
Back
Top Bottom