My goal is to detect when music starts playing on the device. In my case I want to launch volume controls on an Android Wear device but that's irrelevant for the question.
I know there is AudioManager.isMusicActive() but it requires polling. I would rather listen for a broadcast without keeping a service alive indefinitely.
The other alternative would be to listen for headphones being plugged in but apparently Intent.ACTION_HEADSET_PLUG is only delivered to dynamic receivers as this answer suggests.
I'm a bit clueless here. How can I listen for any audio related events without constant polling?
Related
We have a medical watch which continuously produces measurements. The measurements need to be analyzed offline in the cloud, and are uploaded from an application which communicates with the watch using BLE.
What we do today is we run a service as a short foreground job every few minutes, extract as much data from the watch and then go back to sleep.
We’d like to speed up the process, and also receive alerts from the watch in real time. Is using Android’s CompanionDeviceManager the solution to this? Will it allow the application to receive notification while it’s in the background?
CompanionDeviceManager does not directly affect GATT notifications in any way.
Whenever any device has been paired through the CompanionDeviceManager by the app however, the app gains these permissions:
The app can start activities, turning on the screen, while the screen is turned off. See https://developer.android.com/guide/components/activities/background-starts. This was always allowed up to Android 9, but for Android 10 and later this is now restricted and having a paired device this way is one way to lift this restriction.
https://developer.android.com/reference/android/Manifest.permission#REQUEST_COMPANION_USE_DATA_IN_BACKGROUND. I guess this relates to the use of mobile data traffic in the background.
https://developer.android.com/reference/android/Manifest.permission#REQUEST_COMPANION_RUN_IN_BACKGROUND. It's a bit unclear to me exactly what this permission does, but at least this can help you start a foreground service while in background.
The CompanionDeviceManager doesn't really affect the Bluetooth API or Bluetooth communication in any way. The important thing in order to have working GATT notifications is to not get the app process killed, which can example be done by making sure a Foreground Service is running in the app process.
Hello dear Android experts.
I have a simple question: is there any broadcast event I can listen when microphone detects a sound?
Or, if NO (the preambula of the question) is there a way to run ordinary service as long as user need. We have a microphone class which check periodically microphone activity, it runs in service, but Android OS kills it about hour.
I am trying to create widget that can control music playback. Basically the same as the widget that appears on lockscreen when music is playing. (Which as I read somewhere, is connected to the Remote Control Client - is that true?) My problem:
I was able to create Media Buttons using the following code
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
sendOrderedBroadcast(i, null);
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
sendOrderedBroadcast(i, null);
First, I thought this was the solution used also in the lockscreen widget. But then I noticed that this solution works only with some music players, but certainly not with all that can be controlled by default lockscreen widget(f.e. DoubleTwist responds to lockscreen widget, but not to my Media Buttons). So I spent last few days digging in the Android sources, Logcat outputs and various forums, but I was not able to find any difference between intents called by my buttons and by buttons on the lockscreen widget.
What exactly is lockscreen widget doing to control apps that are not listening to my Media Button intents?
Or, can somebody at least help me to find the source code of this widget? I tried default music app, audio service, remote control client, widgets, but I can't find it anywhere.
While working on my app I've actually found how to implement your own RemoteControlDisplay which can control music player the same way the lockscreen does.
Basically, you extend IRemoteControlDisplay$Stub, which sends messages to special handler, this handler updates metadata and thing. Then you register your own class extended from IRemoteControlDisplay$Stub by calling to AudioManager#registerRemoteControlDisplay().
And then you unregister it by calling AudioManager#unregisterRemoteControlDisplay().
It's fairly complex, but I've wrote an article on how to this.
I've published it on XDA, check it here:
http://forum.xda-developers.com/showthread.php?p=44513199
What exactly is lockscreen widget doing to control apps that are not listening to my Media Button intents?
Based on the docs, it is doing what those apps asked it to do -- execute the PendingIntent supplied to it by the RemoteControlClient. Notably:
it will not be an ordered broadcast, as PendingIntent does not support it
it may be one targeted at the specific media client, via setComponent()
it may or may not have the extras you are trying (incorrectly) to use
(The "incorrectly" part is because you are sending two ACTION_UP operations some of the time, as sendOrderedBroadcast() is asynchronous with respect to the calling thread, and therefore you may be replacing your ACTION_DOWN with ACTION_UP before the first ordered broadcast is sent. You are better off using a separate Intent object for each broadcast.)
However, while the docs claim that the PendingIntent needs to be set up for ACTION_MEDIA_BUTTON, I would not be surprised if this is a documentation error, and that no specific action is needed, as setComponent() is sufficient to deliver the broadcast to the right receiver.
I am working on a Security application, where i need to play siren music on receiving certain text(say "siren").
So far, I've been able to receive the SMS intent and play the .mp3 siren music. But the problem encounters when the device is switched off.
Its been to my knowledge that there are some Intents(dont know what exactly to call them), those intents are fired even when the Device is switched off, just like the Scheduled Alarms (which executes even when the phone is switched off)
If anyone knows about those services or whatever they are, it would be helpfull if you share those ideas.
If you need device to be awaken when you do your tasks, then simply wake it up using PowerManager. Please note that once your onReceive() is completed device may fall back to sleep, so if you spawn some other processed in your BroadcastReceiver, you need to hold WakeLock.
You may take a look at this as well: https://github.com/commonsguy/cwac-wakeful
Maybe this might help.. Shouldn't be that hard to do but how can you get a text message if the phone is turned off?!
http://developer.android.com/reference/android/app/AlarmManager.html
I have a service that plays a notification sound. The sound is user defined so it can be anything. If it has a long play time I want the user to be able to stop it by pressing any physical button on the phone. How do I go about setting up my OnKeyListener?
This is not possible in general.
If the device has a CAMERA or MEDIA button (latter being on wired headsets), and if the foreground activity does not consume the event, then there is a broadcast Intent (ACTION_CAMERA_BUTTON and ACTION_MEDIA_BUTTON, respectively) that you can listen for. However, relatively few devices will have one of these.