I want to write a test case that spans over multiple activities(3 to be precise). I need to start a service which will be used by the all 3 activities. How could I start the service from my TestCase using Solo? Also after the service starts would I have to take some special steps to bind the activities to the service? Sorry I am an Android noob.
If you start your service along with your app. you will not have anything to worry about one the activity launches. Alternatively you could add a 4. activity which starts the service and then trigger this activity from Robotium prior to the "real" test.
Related
I want to write an android service that will run with an app. I want to run this service across multiple activities and have the ability to pass information back to the UI with all activities, not just the activity that starts the service.
From what I understand, I have call startService and not bind it to the activity. Is this correct?
Also, I do not want to start a new instance of the service, I want to use the same instance. If i call startService in each activity, will that use the same, running instance of the service?
startService will start the service only if there's not a previous service running, it won't spawn a second service.
Once you start your service, you can connect to it from any Activity you want, but to pass information back and forth, you need to bind to the service, so you can obtain a pointer to it.
Make sure to read Android's documentation, because the way services work is a little confusing at the beginning.
How to detect user activity/inactivity in android service/background application - from same or other applications?
I want to know that user stop touching screen and not busy with another activity to show notifiation popup. Not sure is there no other method than use AccessibilityService.
One of the ways is to know that from reading the Android logs
Use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver.
I think they answered it in this question to do what you are looking for:
android: running a background task using AlarmManager
Define a interface in activity and service.
in oncreate of activity set reference to activity in service method.
Now you have activity reference for entire service lifetime.
another way to do this is, application class, same application class is shared by activity and service, so you can both of these from each other using application class.
If you want see from applications then use broadcastreceivers that is the only wau
I need an advice for my latest app. It will show the user the latest subtitles released, and it will give him a notification in case new subtitles of his favourite series have been released; what should I use to achieve this?
I was thinking to create and run a service which will include a
timer.scheduleAtFixedRate(new TimerTask() {...
but at the same time I really don't know how to make it interact with my app (if the app is opened I don't need any notification but I need to update the GUI).
I could use a thread but I'd like it to run it even after the main activity has been killed...
or I could use a AsyncTask so it would be easier to deal with the Application GUI.
Which solution should I use? I was thinking I should simply use a service (the first solution), but I'm not too sure about it, and furthermore I don't know if there is any way to make a service communicate with an activity periodically...
Thanks for your help.
A service communicating with an activity is called bound service, that's what you should use IMO.
However, you say that when the activity dies, the service should keep running. But if the service is bound to your activity and the activity is dies, the service dies too.
I suggest you to read here about it.
Check and see if you can bind a service to an activity, and when it dies, unbind and let the service continue to run independently.
If you can't, the activity could unbind itself, then start the service independently (with startService rather than bindService).
if you are showing notifications, why not use C2DM messages for communicating with the app. The only thing would be that there would be popups shown to the user even if your app is not running. No need to use threads/services.
Is there a possibility to autostart Service upon starting Application ? The problem is that I am developing separate UI component that depends on service. Ideally this service should be started as soon as hosted application starts. Could this be done via manifest only or could it be done at all ? I know I can start service from my UI component's code, but I want to start service immediatelly after starting main application even in case if my UI component hasn`t been created yet.
Thanks in advance.
Create a MyApp class which extends Application, and make sure it's declared in your manifest. MyApp's onCreate() is then a good place to start the service if you need to.
See the documentation for the Application class.
You want to use the PERMISSION that notifies you of boot completion. This will allow you to know that the device has started and take action, such as start your service.
RECEIVE_BOOT_COMPLETED Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.
Hi
I am trying to use several services in an application, i wish to re-run the services after a certain period of time after fetching the changed values from one of the services.
Pls guide me on implementing this using handlers/timers/broadcast receivers.
Thanks
You should use an AlarmManager that sends pending intents. You can use multiple intents and broadcast recievers. In the broadcast recievers you can start a service. This way you can start multiple services at different times or together if you want.
You can use the Alarmmanager to send repeated alarms, or you could use it in one of the services and set a new time for your pending intent.
Do not use a timer!
The benefit of doing it this way is that this all works when the Activity is not active.
You can bind to a service as shown in the example, but if you want to pass data from a service to an activity you can just use the application object, or a singleton. Also I would use an IntentService, which automatically does its work of the main thread. If you want to do something on a different thread that should more closely interact with the Activity I would use an AsyncTask or a Thread.
The following page describes to you in detail the working on a broadcastReceiver class and a Service at start up.
http://www.androidenea.com/2009/09/starting-android-service-after-boot.html
it also implements the Timer functionality... but please be advised that the timer in that method is really fast and makes your emulator unresponsive... or slow.
and for the messaging part please refer this thread.. the Program given in this thread is ultra cool.. kudo to the programmer...
Example: Communication between Activity and Service using Messaging
Regards,
--Rajesh