I've decided to use a service for the Android Studio app I'm working on; however, I can't seem to find a way to reference the service in my project. The service will enable the mic to continuously record and using a thread, notify the user if the sound is over a certain loudness and then update the phone's location accordingly. So within the service, if the mic listens into something above a certain loudness threshold it will pass the information to the Google Maps activity. The service is a started service that starts after a button in another activity is pressed.
Is there a way to reference the service, possibly a variable within the service, from my Google Maps activity java code? Do I have to use a bounded service instead?
You describing a service that should have connection to ui, notify it about it state and possibly allow changing it. You should you use bounded service for that, they have exactly what you need: https://developer.android.com/guide/components/bound-services
Related
I want to inject a touch event Android Studio during app running at background.
After searching it, I found a couple of solutions but all of them need root rights.
For my private use, I am trying to make a remote control app (like TeamViewer Quicksupport) that streams to my video streaming server RTMP&RTSP and I need to inject tap (via touching) and input (via typing).
I was trying to create a service that can interact with the AppUI when the App is opened and continues its working when the App is closed.
I have heard that the bindService() call creates a Bound Service that can communicate with the App. At the same time, Some resources say that the startService() can be further made a Foreground Service by calling startForeground() inside the onStartCommand() callback, and then use them to communicate with the App.
Can anyone give the best way to achieve my need and the difference between this?
From the Official Documentation: https://developer.android.com/guide/components/services.html
Bound
A service is bound when an application component binds to it by
calling bindService(). A bound service offers a client-server
interface that allows components to interact with the service, send
requests, receive results, and even do so across processes with
interprocess communication (IPC). A bound service runs only as long as
another application component is bound to it. Multiple components can
bind to the service at once, but when all of them unbind, the service
is destroyed.
Foreground
A foreground service performs some operation that is noticeable to the
user. For example, an audio app would use a foreground service to play
an audio track. Foreground services must display a status bar icon.
Foreground services continue running even when the user isn't
interacting with the app.
It looks like you should use Foreground Service.
I want my app to get updates of the current location, and act upon it. I am planning on creating a service, that will host the Google API client code and location update code.
The name though throws me off: Is google location SERVICES a service that will automatically run in the background, like the service I was to create? Or is it just a name and I should go ahead with my plan?
You're not thinking about this correctly - it doesn't matter how Google implemented it. To directly answer your question, I can say with near certainty that it's not called "services" because it's a Service - the two have nothing to do with each other. Google likely uses a whole series of Service's, but you don't have to care about that.
All that you need to know is that it works - for as long as you request updates, and obviously the frequency and accuracy will depend on the filters/providers you request, you will continue to get updates. You do not need to maintain a Service in the background to continue to receive updates. You only need to maintain a Service if you need to receive and continually do something with those updates while your app is in the background.
I will also say that this can come as a disadvantage. If you request location updates and never make the api call to stop receiving updates, you will cause your app to hold a partial wakelock on the system, and you'll kill your users' batteries. Once you've got the location info you need, make the proper call to stop receiving updates.
Think about what you need location for, how often you need it, etc, and know that Google location services will continue to call your callback object as long as it hasn't been garbage collected.
I am using Google Voice in my android app, and I am connected the app to Bluetooth module, each time I call the Google Voice, the app try to connect to Bluetooth again, each time Google voice window Popped up, the connection lost, and once the window disappear, and the connection comes back again. So, Is there any way to keep them connected even I call the Google Voice window? Any ideas or help I will be appreciated.
You need to create background service for your problem. If you need persistent connection with your Bluetooth you can use intent service. If you are using android studio then it is very easy to create service. Just make sure you must have all necessary information about services and how to make then persistent in background.
Because of this your application will drain your device battery drastically. Make sure service is closed when application exit or all activities are finished.
See this link for more information
https://developer.android.com/training/run-background-service/create-service.html
Place the Bluetooth related processing in a Service. Make the service foreground service.
Activity can connect/disconnect from service anytime.
i'm trying to build an application that every given time, will connect to a remote server and get JSON object from it.
as i searched the web for answer i wasn't able to realize exactly how to setup a service and run it as long as the application is running.
i want my main.xml screen have some kind of TextView which will update from the service.
couldn't found anywhere how to build a service which update a TextView when needed..
i'm looking for a simple example - as for i'm newbie to android development.
What you should look for -- perhaps instead of a service in this instance -- is an AsyncTask. This is what you use when you need to update the UI from the background and not hang around too long on the main thread. Here's one AsyncTask tutorial, and here's what the Android SDK docs have to say about it.
If you need to do things like download JSON every so often from a server, a Service might be a good solution. To communicate back and forth between a Service and an Activity you will use a Messenger and Handler example. You can find an example of how to use the messenger / handler pattern for services and activities in the API demos included with the SDK (here). this SO thread is also relevant.
If you need to keep your service running every so often, you'll want to look at using an AlarmManager to grab the data, store it somewhere, and then refresh the display in the Activity (perhaps through a database in your app). But basically, if you need to quickly download some stuff and update an Activity, use an AsyncTask, if you need something longer term, bind a service and then communicate back and forth between it and the Activity using a Messenger / Handler pair (or AIDL, but that's more complicated..)
You might be able to use a bound service to achieve it. http://developer.android.com/guide/topics/fundamentals/bound-services.html