The handling of starting service from application and boot receiver - android

Scenario:
I start a service at boot time.
I want to start the same service at main activity manually.
I got 2 Questions:
1. How to ensure my manual service started is the same as the one at boot time.
2. how to retrieve those running services
Thanks

I start a service at boot time
Why?
How to ensure my manual service started is the same as the one at boot time.
Use the same Intent structure.
how to retrieve those running services
There is no such concept in Android. For starters, there will only ever be one copy of your service running. If the service is already started, and you call startService() again, it does not start up a second copy, but delivers the Intent to onStartCommand() of the running service instance.

Related

Service starts activities from different Apps

I developed two apps(can be more in the future) which are using the same service. When I start one of the apps they should start the service(if not already started) and send the name of an activity to tell the Service that the app is available.
The service is located in a third project, which is implemented as a library in the apps. The service listens in the Background for an event and start one of the apps.
The problem now is, how I start the service.
I tried already startService and bindService:
startService:
Intent intent = new Intent(this.getApplicationContext(), DriverBackgroundListener.class);
intent.putExtra("className", "com.example.testProject.Activity1");
this.startService(intent);
Starts a new Service for both apps, because the service is not located in the same app. So 2 Services are running and listen for the same event.
bindService:
I implemented it with the help of a Messenger. (https://developer.android.com/guide/components/bound-services.html)
The Problem here is, that the Service will only run if minimum one Activity is bind to the service. But I need to finish the Activity so the user can use the device normaly. But that will unbind the service and shut it down.
I also have to run the Service after a reboot. But I guess that will work if I create a BroadcastReceiver which starts the activities after the device is booted.
Have anyone an idea how to handle this?
Thanks best regards
Fabian
If you start service and then bind to it with flag 0, it still work even after you disconnect from it. You can try to connect to service first and make a check in onServiceConnected(), and if not start service. Also take a look at AIDL https://developer.android.com/guide/components/aidl.html.

Keep a Service started at BOOT running even if Application stopped

I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.
The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.
The activity has the properties:
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleInstance"
so that it does not show up in the task list (when user long presses the home button).
The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.
So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around??
I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.
I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.
There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.
The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:
http://developer.android.com/guide/components/services.html#ExtendingService

Service shouldn't stop when the phone is sleeping, or the activity (for starting the service) is not running

I want to run a service to collect the accelerometer sensor information and it shouldn't stop when the phone is sleep or the activity (for starting the service) is not running.
I have to send start and stop commands to the service from the menu activity.
currently I am using a bundled service in the same process of the activity but the problem is that it gets closed as soon as activity is closed (return key pressed).
I am wondering if I use a separate process it will resume even if there is no bundled activity (when activity is closed).
If not, which service model should I choose?
You are probably looking for startService instead of bindService.
http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29
However, even with startService, there are no guarantees the service will remain running "forever" and "always".
WARNING, the options below will consume a lot of battery.
You can increase the chances the service will not be stopped by changing the priority to startforeground (requires a notification).
While the screen is off, the only way to keep the service "alive all the time" is to use Alarm Manager with an RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP schedules.
Less battery...
Practically speaking, however, without startForeground and just using normal RTC or ELAPSED_REALTIME alarm schedules, your service will run most of the time.
You can create a service in the same process with your application, even if your activities all closed, the app still work because your service still alive until you call stopservice (the system will restart your service automatically when it is killed by system). if your service perform complicated communication with activities , i think you should use remote messenger service. During running of service you can bind to service to send and receive data between service and activities.
For more information of service and communicate to service, you can refer here

how to start a service properly and keep it alive?

I know there are other question with the same topic, but I didn't find an answer to my questions.
my goal is to have a service which works on the background as a location listener, and it won't be stopped when the application is stopped (either by a task killer).
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
now, the first problem is that my application crashes but the service has started, and when I run the application again it works.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
and it won't be stopped when the application is stopped (either by a task killer).
Fortunately, this is not possible. If your user wishes your service to stop, the user can stop the service via a task killer or the Manage Services screen in Settings.
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
Usually, you only use one or the other, not both.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
No, because you called startService() in addition to bindService().
The service stops when the application is closed by the task manager. If this could not be possible every app would have its own service running without any user control over them. You could start the service at boot up and then when the user uses task manager to close, you could restart the service.

How to register a Service that will run all the time

I would like my android app service to run all the time.
that is -
1. right after installation,
2. on boot
3. if its closed - it will be relaunched -
how do i achieve all of the above code-wise?
thanks!
I am not putting the code here however you can easily find it.
Right after installation use the default activity to launch the service, in case you do not have any UI then create an activity without any UI (no setContentView) and in its onCreate start the service.
You need to create a broadcastReceived that listens to ACTION_BOOT_COMPLETED and call that as Service Manager. On Receiving the broadcast in that receiver just start the service again.
Make your service as foreground and that should ideally take care of this scenario.
You shouldn't use the Service foreground feature! The best practice in current android version is to return START_STICKY from your Service's onStartCommand(). It will cause the Android system to re-launch your service.
Regards.

Categories

Resources