I have a broadcast receiver listening called PACKAGE_ADDED and an other broadcast receiver listening called BOOT_COMPLETED. The bootcompleted broadcast receiver starts my service.When the new app is installed, I want to send a message to my service .The first solution that came to my mind was to start the service again with
intent.setAction("NEW_APP_INSTALLED");
startService(intent);
without stopping the service and check the intent.getAction() value in theservice.onStart() method. If the result is NEW_APP_INSTALLED, then call newAppInstalled().I don't think this is an elegant solution.
Is starting the service repeatedly a problem? And what happens when my activity binds to it via ipc(aidl) while fetching data and the new app installed broadcast receiver starts it again? Lastly what is the best way to solve my problem?
Is starting the service again and again problem?
Lastly what is the best way to solve my problem?
It is perfectly fine for you to call startService on an already running service. And you can either do it the way you suggested or have two different services (one for boot, one for new_app) or you can register a BroadcastReceiver in the service after it's started, but that wouldn't be effective because then if you try to send a message to it and it's not running already, it won't get the message.. I prefer one service as you suggested and using startService.
And what happens when my activity bind it via ipc (AIDL) fetching data
and new app installed broadcast receiver starts it again?
Well, I don't know anything about AIDL, really. This might help. That page does state "Most applications should not use AIDL to create a bound service". This is because it makes multi-threading needed and makes it more complicated.
Please let me know if I failed to answer to your satisfaction - though I can't really elaborate on AIDL specifically because I don't know anything about it.
Related
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.
this is what I've gotten so far:
Service running which is keeping up two connections (to two different servers), registering a BroadcastReceiver. The BroadcastReceiver is receiving my commands, which I want to send through the sockets. Working so far.
But: If I send "more" commands in a short time span (eg multiple commands in 1 sec) the BroadcastReceiver does not receive them - is the broadcast receiver too slow? Would it help to start a different thread in onReceive for handling the extra data?
Or should I go back to binding the service and passing direct commands to that object?
Would this be possible? -> Service running in background, registering a BroadcastReceiver, but also bound to an activity - it should still be the same service "object", right?
Thank you for your help.
I'm not 100% sure but instead of registering the broadcast receiver within the service code, it may be worth registering it within the Android Manifest may make it a bit quicker. This is how I usually do it and never find the broadcast to be slow or not received.
I am writing here after countless hours searching the net with no gain.
I am trying to create an app which starts a service, this service will have a broadcast receiver in him which should detect when a phone call is received, than this receiver calls a method inside the service with the calling number.
Can anyone help me out here? what should i be looking for? in short how should i handle this?
Check out this question. Sounds like for you, you might simply use a singlton instance of your service, or perhaps a broadcast receiver. Definitely stay away from AIDL.
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.
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.