Is there anything wrong with this way of trigger an action in an Activity 
from a local Service?  It seems too easy.  But all the examples I have seen 
of accomplishing the same end use a more complicated Messenger method.  
Anyway, here is what I do:

In my activity, I start and bind to a local Service (BTService) from 
onResume:

    Intent intent = new Intent(this, BTService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    startService(intent);

The "mConnection" referenced above is defined by:

/** Defines callbacks for service binding, passed to bindService() **/
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get BTService 
instance
        MyBtServiceBinder binder = (MyBtServiceBinder) service;
        btserv = binder.getService();   //..Activity's reference to the 
Service
        btserv.main = Main.this;        //..Service's reference to the 
binding Activity
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        if(btserv != null)
        {
            btserv.main = null;  //..Break the Service's reference to the 
Activity
            btserv = null;       //..Break the Activity's reference to the 
Service
        }
    }
};

In my Activity's onPause I disconnect from the Service as follows:

    if (btserv!=null) {
        btserv.main = null;  //..Break the Service's reference to the 
Activity
        btserv = null;       //..Break the Activity's reference to the 
Service
        unbindService(mConnection);
     }

So when the Service wants to trigger an event in the Activity, it does this:

     if(main != null)
    {
       main.handler.post(main.doHandleEvent);
    }

Where "main" is the reference to my Activity that was set by the Activity 
in "onServiceConnected", and "handler" is a Handler in my Activity that 
handles posts to the Runnable, "doHandleEvent".  Then in short order, the 
doHandleEvent in my Activity will run.  And that is how I trigger an event 
in the Activity from my Service.  Of course there are many assumptions in 
this approach.  For example, only one client should connect to my Service, 
since it only holds a single reference to the client.  And the triggering 
shown above is from the UI thread, not from a worker thread.  That gets 
around the race condition where "main" is not null at the "if" statement, 
but becomes null by the time the thread gets into the referencing of 
handler and doHandleEvent.  I have tested this method in a Bluetooth 
application and everything seems to be working fine.  But I want to know if 
there is some "gotcha" that I have not seen.

Robert Scott
Hopkins, MN

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to