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!
A service explaination. You can bind to a service and call methods from it.
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.
And simply stop the service by calling.
This is the serviceConnection I use.
Place this in your service to return itself to the activity:
Finally, this will be called after starting your service
startService(intent);
Note that it returnsService.START_STICKY
. This will prevent your service from being killed, unless the OS has very little memory.I hope my answere will help you and good luck with it.