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.
Related
I have a food ordering app and I need to inform the restaurants of a new order. I have a Capacitor app which often runs in the background of tablets/phones of the restaurant. As a result, they sometimes miss an order.
In order to solve this, it would be great if I could ring the device as if an alarm goes off or if the device gets a call. Then they can swipe away the notification to stop it or something like that, to make sure they saw it. I would choose the sound myself so that it isn't obnoxious.
Is anything like that possible?
yes it's possible but,
you should know the following:
for Android:
1-create a foreground service to keep notification appears to the
restaurants, in this case the restaurant can not hide the
notification.
once the restaurant receive the notification just
sent an event to the foreground service to handle the action and the
data.
start the order activity from foreground service, once the
activity is created just play a sound.
for IOS,
it should be the same but i don't know how to create a foreground service in IOS.
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?
I'm making an application that run in background after install and do some task as volume down key is pressed at any point of time in Android. I have created a service of it but it doesn't listen to key event as application closes and run in background. Application is not for media player.
See the following:
Volume change Listener: Is registerMediaButtonEventReceiver preferable to onKeyDown?
You can register to this event from a service and that should do the work for you.
This requirement is only satisfied if the app is running in the background.And if the Screen is turned on if the user presses to check any notifications then an Asynctask is called if the app is running in the background and makes a call to the server.
I have tried using Broadcast Receiver when screen on and tried to execute, it works only if the app is on the front screen after pressing the home button .And then if the user presses Power button after an hour then nothing happens .
Basically I am not sure if the app is being killed after sometime when in background. Please help me.I am a noob in Android and this functionality is something I thought most of the developers might be using but I did not see anything except service calls and I really did not want any service/alarm-manager as I don't want it to work continuously.
TIA
how to make server call whenever device screen is turned ON without Service
This is not possible. ACTION_SCREEN_ON is a broadcast that can only be received by a BroadcastReceiver registered via registerReceiver(). So, unless you are the foreground activity, the only way you can receive this broadcast is via an always-running service, which is not a good idea.
as I don't want it to work continuously
Then do not "make server call whenever device screen is turned ON". Find some other solution for whatever business problem you have that you are trying to solve this way.
i have read for hours about how to start play a sound the way i want
but i cannot find an answer.
Here goes,
When device receive a C2DM push im displaying a Notification.
User can click the Notficationand my activity start.
Everything works grate.
Now I would like to create a phone-call-type of ring-sound
so it sounds like there's an incoming Voice call.
User pick up phone and notice my Notification presses it
and the sound stop playing.
Or even better, the C2DM message trigger my activity to start
with a phone-call-type of ring-sound.
When user touch the phone the sound stop playing
any help in any direction would be grate
Set the sound data member of your Notification to a Uri pointing to the ringtone you want to have played when the Notification is displayed. Ideally, you let the user pick their own ringtone (e.g., via a RingtonePreference) rather than forcing a certain ringtone on them.
Or even better, the C2DM message trigger my activity to start with a phone-call-type of ring-sound.
Popping up an activity like this, when the user might be in the middle of something else, is infrequently a good idea.