Search code examples
androidservicebroadcastreceiver

Connect my Activity, Service and BroadcastListener


Developing my first Android app at the moment. I'm new to Android, but did a bit of Java in University. PHP, JavaScript, C# and my employers proprietary language (not unlike BASIC) are my background so I do have a good programming understanding.

The plan is:

  • An Acticity with a toggle button - when on it starts the service (below).
  • A Service that will sit in the background and listen for data on a specific TCP port.
  • A BroadcastReceiver that will monitor the WiFi status.

I have all three in place - but can't seem to figure out how to connect them!

The Service should be able to send data back to the Activity. The Service should also stop if the Activity is terminated (but not if it is just closed - only if the app is terminated.

The BroadcastReceiver should be able to stop the service and update the Activity if the WiFi changes to disconnected.

At the moment all three work semi independently of each other. The Activity can stop and start the Service no bother. The BroadcastReceiver will show a Toast message if the WiFi is disconnected. Its just the inter-communication I'm not sure of.

The app is a personal project and I will be open sourcing it when complete. As such I'm more than happy to share the current code with anyone who can assist. It's at http://www.tip2tail.co.uk/files/android_code.zip

Any help is appreciated :) Thanks!


Solution

  • A service explaination. You can bind to a service and call methods from it.

    Intent it = new Intent(MainActivity.this, MyService.class);
    bindService(it, mConnection, Context.BIND_AUTO_CREATE);
    

    Bind to the service in each activity so you can do whatever you want with it. Don't forget to unbind in your activity its' onDestroy.

    if(mBound)unbindService(mConnection);
    

    And simply stop the service by calling.

    stopService(new Intent(this, MyService.class));
    

    This is the serviceConnection I use.

    private ServiceConnection mConnection = new ServiceConnection() {
    
        @Override
        public void onServiceConnected(ComponentName className,IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;            
        }
    
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    }; 
    

    Place this in your service to return itself to the activity:

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    public class LocalBinder extends Binder {
        MyService getService() {
            // Return this instance of LocalService so clients can call public methods
            return MyService.this;
        }
    }
    

    Finally, this will be called after starting your service startService(intent); Note that it returns Service.START_STICKY. This will prevent your service from being killed, unless the OS has very little memory.

    public int onStartCommand(Intent intent, int flags, int startid) {      
        return Service.START_STICKY;
    }
    

    I hope my answere will help you and good luck with it.