edit
It think all this stuff is now well out of date, there have been many changes to this as android has developed. I'll post an answer when I have worked it out (if noone else has).
I really just want a background app to get first crack at bluetooth messages.
end of edit
I have an android app that needs input from the user - I want to use bluetooth headset buttons (actually bluetooth watch/band buttons - but technically the same!).
I have been looking at media button examples, there are many here and elsewhere which all basically say:-
1) Put receiver/intent in manifest
2) Register receiver in main activity
3) Do processing in the receiver class
This isn't working for me (even cutting and pasting sample code), with no errors, but no events being triggered.
However I also found this:
http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html
Which is a dedicated bluetooth headset api... has it superceded the media button approach? or is it just higher in the food chain? Should this be used instead of the Media Button Event stuff - or are they just different perspectives?
If the media button stuff is the way, I can post my non-working code, but don't want to waste anyones time if its an out of date approach!
Thanks!
For examples of using bluetooth buttons - this is an excellent example...
https://code.google.com/p/media-button-router/
It detects a hit on the headset 'play' button then pops a dialog asking which app to direct the 'play' to - it plays the choices via the headset so one can be selected without viewing the screen.
Related
I'm new to android app development and just creating a simple application for my workplace that allows video playback control from a web panel, so the app pings the panel on a loop and if it gets a request to play a video, it plays it. If it gets a request to stop the video or play something else, then it does that.
From MainActivity I'm using startActivity to start an instance of ExoPlayer, I know how to pass variables to the activity, but how do I send information the other way or control the playback? Basically that loop that runs constantly to check for new actions is running in MainActivity, once the player is started I have no ability to stop playback, get stream metrics or do anything with that instance at all. I realise I'm probably doing things backwards and should have a background service do the checks, but I still have no idea how to pass information back and forth.
Does anyone have any tips or suggestions? Thank you!
Instead of using two activities, you have to use an acitivty (the main) and a fragment (the video player). Then define a callback interface which will implement the activity and thus manage the communication between fragment and activity
See this https://blog.mindorks.com/how-to-communicate-between-fragments/
I'm trying to make my app add a toogle button in the [Notification Panel] the place where you can toggle Wifi, bluetooth, Plane Mode, ect .., (I've spent the last 6 hours searching and I found nothing)
Here's are 2 Examples of what I'm talking about in one picture :
-In the 2 blue frames, those 2 buttons are custom buttons (meaning they have nothing to do with stock android, they've been added by apps from the Playstore),
#1 button : The Shazam button is a shortcut to open shazam (that's not what I need, although would love to know how it's done)
#2 button : First press it starts recording a video on my rear camera (the button becomes activated (blue)), second press it stops recording and saves the video, all this happens in the background, the app that does this never opens (meaning that this is not just a shortcut to the app)
=> So I'm trying to do is to have a custom button for my app, a button like the #2 button, for my app activating/disabling it would make my app's background service send TCP messages to a server, (just in case you want to know what I'm trying to do)
My phone is not Rooted, and I know for a fact that this doesn't require a Rooted phone.
(I'm trying to do this with Xamarin/c#)
sadly no one answered, so i kept searching and i found out that what i called the Notification Panel Menu, was in fact called the "Quick Panel" ..
anyways, here are some links that helped me quite much : Link 1 , Link 2
I am developing an App that has a lock screen widget where it has a play, pause, prev and next. When I connect my phone to an Android wear, it automatically shows this buttons and it works fine. I would like to add a "Like" button that would automatically be shown on the lock screen and the android wear. I know that I can create a Notification and add action for the like. But I want to know if is it possible to achieve that using only RemoteControlClient?
Have you tried using rating with the flag FLAG_KEY_MEDIA_RATING ? It seems that it's the only way to maintain generic methods and avoid having to set custom code on the wear side (or through notifications).
This flag can be set with setTransportControlFlags
Flag indicating a RemoteControlClient supports ratings. This flag must
be set in order for components that display the RemoteControlClient
information, to display ratings information, and, if ratings are
declared editable (by calling addEditableKey(int) with the
RATING_KEY_BY_USER key), it will enable the user to rate the media,
with values being received through the interface set with
setMetadataUpdateListener(OnMetadataUpdateListener).
Unfortunately, few or not example exists on the web. I discovered only one (unanswered) question relative to this on SO :
Android 4.4 KitKat Rating API
I have an Android app with 9 buttons. This app runs on 2.36 and is the only app on the device (or at least the only app we let the user use - we ship the device with our code preinstalled as part of a suite of industrial products we sell.)
All the buttons go to the same handler and get sorted out there by their tag. The handler is specified in the XML:
<Button android:id="#+id/IdleButton"
android:layout_marginLeft="5dp"
android:background="#drawable/idle18pt_he_normal"
android:hapticFeedbackEnabled="true"
android:layout_width="92dp"
android:layout_height="92dp"
android:tag="0"
android:onClick="theButtonHandler">
</Button>
I want to enable haptic feedback, i.e., a vibration, when the user presses the button. Is there a way to do this just in the XML, or if not, is there a way to do it in my onClick() handler?
The web examples I've seen (e.g., http://androidcookbook.com/Recipe.seam?recipeId=1242 ) for haptic feedback on Android mostly seem to involve changes to the manifest, changes to the XML (you can see I've already enabled it in my XML, above) and then declaring, initializing and implementing a separate Touch handler for the button. This seems like a lot of work, especially since I have 9 buttons.
Since I already have just one onClick handler for all my buttons is there a way I can implement the haptic feedback there?
All I had to do to get a "click" sound when I tap one of my buttons was to checkmark "Audible selection" in the "Sounds" part of the phone's settings - no coding at all. Why is haptic feedback so much more complicated?
Without using the VIBRATE permission.
You can use performHapticFeedback() function of any View including Button.
For example programmatically in Kotlin:
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
This vibrates the device.
You can vibrate the device from anywhere in your Fragment or Activity, even when you don't have a View available.
In Fragment you can do:
requireView().performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
In Activity you do:
window.decorView.rootView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
Create a custom VibrateButton class that inherits from Button, and add this vibration onClick. You'll still need to ask for permissions in the Manifest, so there's nothing much you can do without inheriting. This example code is taken from here, and does the vibration.
import android.os.Vibrator;
...
Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
Note:
Don't forget to include permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
The reason sound can be automatically set for buttons, but vibration can't, is the same it asks for permissions to allow vibration. Vibration consumes battery (way more than a simple sound), and so it affects battery duration, which needs to be, somehow, approved by the end user. If you see, in most some there's an option to "vibrate on click" for some specific buttons (keyboard apps, mainly), but that's dependant on each app.
PS: Make sure the device can vibrate. I had some hours stupidily lost when adding vibration to a Nexus 7 2012; it hasn't got a vibration module. Also, make sure you can disable that, to keep battery up longer.
I have an app which uses the ACTION_MEDIA_BUTTON intent with a BroadcastReceiver to control a music player. The user pushes a button on external hardware and it controls the in-app music player. The user can also HOLD DOWN the button and change the volume.
I recently downloaded another app which uses the headset button, and it takes over the media button intent from my app! So when this other app is open and I press the button, the other app will start running, but my app will think that the button is still pressed down so it will cycle the volume.
To summarize,
my app is open, supposed to be sole listener of media button intents
other app gets opened, it also wants to be sole listener of media button intents
button gets pressed with both apps open, control goes to other app
my app thinks the button is being held down, as it lost control as the button was pressed in down mode (I think). It then launches functions I don't want launched because it thinks the user has held down the button.
Is there any way I could make sure that while my app is open it's the sole receiver of this media button intent? Could I at least check to see if another app has taken over, so I can prevent unexpected behaviour?
Thank you for any help, I've never had apps not play nicely before!
You can alter your BroadcastReceiver's priority (make it something large, like 10000): it should then get the Intent first, and then you can pass it on to the other app.
I have a similiar issue. I believe, outside of the 'arms race' over the priorites mentioned, the only real solution is to close the other application. If you are releasing this application to other users, you could possibly give them a message telling them to close other media player apps and services.
In your manifest you can set the intent priority to the max value of an integer which is: 2147483647.
You should not however set your IntentFilter priority over 1000 as it tells you in the API docs. You can set the IntentFilter priority like so:
myIntentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
I assume you should subtract one from the SYSTEM_HIGH_PRIORITY constant as the docs say the value must be less than SYSTEM_HIGH_PRIORITY.
Quote from docs about IntentFilter.setPriority(int):
Applications must use a value that is larger than SYSTEM_LOW_PRIORITY and smaller than SYSTEM_HIGH_PRIORITY.