Is it possible to start a service without starting an activity? My plan is to write an application which shows a notification when Bluetooth device is connected and a headset
Thanks for answers
Services ONLY can be started from Android components , such as Activity , BroadcastReceiver and either from another service. Anyway to start a service you need a Context , Because you're gonna call Context.startService(i).
In your case I suggest you to start your service from a BroadcastReceiver as same as this one :
How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY)
Related
How do I start a standard Service on my mobile device from a watch face? Would I need to use the Message API? If so, what would be the correct way of doing this? I currently only start the Service on the mobile's boot and when I open the config activity.
Yes, you'll need to use the Message API. You'll find it well documented at https://developer.android.com/training/wearables/data-layer; if you subclass WearableListenerService on the handheld, it will start automatically when a message is sent from the watch. This could be your existing service (just change it from extends Service to extends WearableListenerService), or you could leave your existing service unchanged and start it from the new WearableListenerService when a message arrives.
In the demo video provided by google, it is said that you don't need to keep a service running to receive updates on activity recognition. But all code examples I could find show that you need to register ActivityRecognition in MainActivity for it to work.
How can I make Activity Recognition independent of application lifecycle?
For e.g. If a user is jogging, show him a notification to record his walk?
Can it work similar to a broadcast receiver which is called as soon a user is connected to wifi?
As an example you could subscribe to ActivityRecognition events when your phone booted successfully. You only need to do it once. After that the PendingIntent is called in the specified interval and you do not need to have a service running all the time in the background.
Creating an application in which whenever Power is connected or disconnected a small ringtone is played.
But the problem that i am facing is that the application is not working whenever i am not taking any launcher activity.
And when there is a launcher activity than the application is working well.
Sigh..vague and no code :(
Android does not allow BroadcastReceiver to receive some broadcast info if the appĀ“s process is not alive.It was designed to against the evil apps. If you have an activity running,your process is alive and so your receiver is allowed to receive the broadcast.
You can make a transparent activity and use startService to start a service in background,then finish the activity.As your service is running ,your process is alive,so the Android will let you to receive the broadcast.
The rest of your questions can be directed at the offical docs.
I want to create a service for my wearable that runs in background that will detect a Bluetooth device. I have written code that is able to detect the Bluetooth device but it uses Activity - onCreate() method to start scanning.
I read many articles on the internet about writing services that runs in background but in every example the service is started from the Activity. I want my service to be running without starting from any Activity. Is this possible? I mean when I install my app is it possible for the service to start running automatically?
Seems like there is no way of starting a service without starting the Activity. I got the answer from there How to start android service on installation
I'm building an app that collects info about the battery (using background service).
I want this service to start running from the moment I turn on the phone, How do I do it?
On the other hand I want to activate the GUI (interface) of the app only when the user clicks on the app. The app and the background service are in the same project.
Is this the correct way to do what I want?
That is the correct way to do it: see http://www.tutorialforandroid.com/2009/07/permissions-journey-receivebootcomplete.html for info about listening for the BOOT_COMPLETED Intent. You can start your Service in the BroadcastReceiver and then bind to it in you Activity.