Android: running a service with a broadcast receiver. - android

I have this app that I want to run at phone start up (As in after the GUI comes up) but I want it to run in the background(like a service)
I know this is possible, since most of the android system is like this.
Any tips would be wonderful!

Create a service in your app. Bind it to an Activity if you wish.
Subclass BroadcastReceiver to listen to the Android.BOOT_COMPLETED system broadcast.
then start your service.

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.

Broadcast on service start?

Does android send any kind of broadcast when a new service is started/restarted.
underlying reason is i'd like to catch whenever a background service is starting without my knowledge.
any answer to how it's possible, if it is, are appreciated.
EDIT:
I want it to work with all services, not only my own services.
If the background service is in your control then you can use onBind() method to get to know that the service is started by bindService() and same time you can use onStartCommand() method if the service is started by startService()....
That mean you want to access system service or what? or service which you have written in your android application?
if you want to control your service which you have written in your application then as above told you can control it..or if you want to start or stop service when you want then you can create broadcast receiver and write onreceive() what you want to do.

Autostarting Android service upon Application startup

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.

Android - What do i need to perform a service in the background

I currently have a service setup that emails a bunch of files. What I want to do is add a scheduling system setup so that at a certain time each night, that service runs (those emails are sent).
I thought maybe a Broadcast Receiver triggered by an AlarmManager would work, and it does except it only runs when the app is running. I read that Broadcast Receivers only run in the UI thread. I need this to work regardless if the app is running or not.
Im going to assume that what I need is a broadcast receiver to start [blank] to run in the background and when the AlarmManager sends an alarm that [blank] will start the service I already have setup.
If that is the correct procedure, what is [blank] ? If its not the correct procedure then what is ?
Thanks
You may want to run a RemoteService (http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html), and this article explains how to use the AlarmManager to start up a Service.
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
I actually made this change today, and my program is working better at work.
The RemoteService is so that the Service doesn't die when your Activity dies, basically.
Your procedure is correct,if you don't need an IPC ,then no necessary to implement a remote 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