Working with broadcast receiver and services. / android - android

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.

Related

Communicating with a Service Thread

I need help with something fairly basic I am having trouble understanding after a few hours of research and reading.
I have:
Service
-Thread
Method: write(string)
I need to pass a variable string from MainActivity to service and call write(string) when string changes. I started to implement a broadcast receiver in the main service but how do I call write(string) method from the main service? Do I need to put the receiver in the thread instead? I'm having a hard time understanding what I'm doing here. Please let me know if there is a better way.
The service contains all the Bluetooth connection and send/receive code. I plan in the future to have another service send data to the Bluetooth service to send to the BT device.
I am still a beginner at Android too but I think what you are looking for are Bound Services. There is also a related question here.

BroadcastReceiver too slow?

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.

Start Again Already Running Service?

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.

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.

Driving a service API from a BroadcastReceiver

I get an error message when I attempt to bind from the onReceive() of a receiver to a local service before I drive a bespoke API on it.
"IntentReceiver components are not allowed to bind to services"
What is the best way of getting a piece of data from a service while in a broadcast reciever.
Many thanks.
P.S. I need to get the answer synchronously i.e. I have wait on the answer from the service so a callback may not be possible.
What is the best way of getting a piece of data from a service while in a broadcast reciever.
If the BroadcastReceiver is created from something else (e.g., an activity) and set up with registerReceiver(), then the "something else" is what should be binding to the service and doing the work.
If the BroadcastReceiver is a component registered in the manifest, then you need to rethink your approach to whatever problem you are trying to solve. These sorts of BroadcastReceivers cannot bind to services and cannot spend much time doing work. If the BroadcastReceiver cannot do its work in less than, say, 10ms, it should delegate control to a service via startService(). That service can then do the work on a background thread.
P.S. I need to get the answer synchronously i.e. I have wait on the answer from the service
See above.

Categories

Resources