the Galaxy S4/S5/S6 Active and the Galaxy XCover models have this extra hardware button. In the settings the user can choose which app the button should open when it is pressed.
I'm developing an app that is specifically targeted at the Galaxy XCover 3. When the user opens the app for the first time, I want to ask the user if they want to let the hardware button open my app.
I have tried to register a broadcastreceiver on the "Camera button" event, but this doesn't work.
Does anyone know how I could achieve this result?
I had the same problem and found a solution.
Use the code below to find the keycode.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
System.out.println("keycode -->" +keyCode);
System.out.println("Key event -->" + event );
return false;
}
Then make a final int with the keycode.
final int ACTIVE_BUTTON = 1015;
And last write your onKeyDown event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
switch(keyCode){
case ACTIVE_BUTTON:
//your action here
return true;
}
}
from Samsung:
Hardware key re-mapping
Refer to this section to integrate a hardware key re-mapping configuration using the Samsung Knox SDK.
PTT key re-mapping considerations
If a PTT app vendor decides to use an intent defined by Samsung, an IT Admin can either leave the intent pre-populated, or enter an intent as provided by their PTT app vendor.
List a generic intent to a PTT vendor app
Vendors can also use the following intents for PTT key press and release actions:
For key press -> com.samsung.android.knox.intent.action.PTT_PRESS
For key release -> com.samsung.android.knox.intent.action.PTT_RELEASE
The following optional timestamp can also be considered:
Extra -> com.samsung.android.knox.intent.extra.EVENT_TIMESTAMP (with type long which will hold the Epoch timestamp of the event)
Secure PTT intents
The Samsung Knox team recommends registering an intent in manifest statically so KSP can wake the app if in a stopped state. Consider the following:
<receiver
android:name=".PTTKeyReceiver"
android:permission="com.samsung.android.knox.permission.KNOX_CUSTOM_SETTING"
android:exported="true" >
Optional Security: To ensure a PTT vendor app is listening to intent actions only from KSP, an app can add the above permission in the receiver, which is platform signature protected.
reference:
https://docs.samsungknox.com/admin/knox-service-plugin/Hardware_key_re-mapping.htm
Related
I have an android application which kind of like a home screen of the build-in screen on android phone. I can list all the available applications and run them. One thing I don't know how to do is when user run an application from my apps, my application can trigger the hardware menu button to show the current application menus.
Is this possible ? If not, what about doing it if the device was rooted.
In activity use override onKeyDown like this :
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
In order to open up the menu from other application apart from the caller application, the device have to be rooted before the code to work.
I'm developing an Android app that intercepts the pressing hardware buttons and makes REST calls to their pressure.
The button that I have to intercept is the Push to talk (PTT) button, so not a regular button, such as power button or volume button.
When the application runs in the foreground I use the method onKeyDown (int keyCode, KeyEvent event).
The PTT button, as the identifier, has the number 27 and then inside the method I wrote the following lines of code:
if (keyCode == 27) {// I pressed the PTT button}
All this works perfectly.
But now I send the application in the background (with the function moveTaskToBack(true);) and when I press the PTT button I would intercept it.
I am aware of BroadcastReceiver, of IntentFilter and Service, however, these allow you to intercept the limited intent actions (such intent.action.SCREEN_OFF or others normal actions), among which are not able to find the pressure of the PTT button.
Is there any way to intercept the PTT button when the application is in the background?
Thank you
The solution to your question depends highly on the device you are using. If you have a phone with a dedicated PTT button, the manufacturer of the phone almost certainly has made an Intent available for app developers such as yourself to intercept PTT down and up events, but you'll need to contact the manufacturer for more information.
For instance, phones from Kyocera, Sonim, and Casio have such Intents available, and you'd simply need to put a receiver declaration in your AndroidManifest.xml, like this for a Kyocera phone:
<receiver android:exported="true" android:name="com.myapp.receiver.KeyReceiverKyocera">
<intent-filter android:priority="9999999">
<action android:name="com.kodiak.intent.action.PTT_BUTTON" />
<action android:name="com.kyocera.android.intent.action.PTT_BUTTON" />
</intent-filter>
</receiver>
Then, a simple BroadcastReceiver class that receives the up and down intents:
public class KeyReceiverKyocera extends BroadcastReceiver
{
private static boolean keyDown = false;
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (keyDown)
processPTTDown();
else
processPTTUp();
keyDown = !keyDown;
}
}
Hope this helps,
Shawn
I am making an app in which if power key is pressed twice,The app will perform some task.Please provide me some help.I have made it but its not working.
I am including my class files here,Please correct me if I am going into wrong direction.
Broadcaster
Listener
I don't know how to insert code here it is giving some error so I included links here.I am sorry for that.Please help me.
Android recommends avoiding double clicks and using the long-click besides the normal click.
For the long click use this :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyLongPress(keyCode, event);
}
You also need to add this to the manifest
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
If this doesnt work properly onKeyDown
But I have never actually used this.
My personal opinion though is that you shouldn't hook the power key if you are planning to publish your app because unless it is doing something really an app shouldn't prevent the user from closing the screen
Solved the problem by receiving broadcast on ACTION_SCREEN_ON/OFF.
When the power button is pressed screen turns on/off depending on the fact whether screen is on or off at the time when pressing the power button.
I used this facility and put the logic in code such that if the user presses power button more than 5 times an sms will be sent to the trusted contacts.
Is there a way to view all intents that are generated by the Android OS's at any moment but maybe filtered by the activity ? Specifically I am testing the onHoverListener - I want to know if my activity is throwing away the hover motionevent or whether none is being generated (system not capable/ some other problem)
Ideally I would like a log of all intents given to my activity - but some other trick is also fine.
Android intents are , capable for starting a new activity , service , complete any action i.e send email , click photo , fetch data from a content provider etc .
Capturing any intent of that kind , you need to have intent filters registered to your activity with the same actions , as that of system intents .[The framework will pick your actvity/app if the same intent is fired and hence you may be able to intercept those intents if the user prefers to] This may be little too much as there will be so many of the actions declared for different android components .But some popular examples are sending sms , picking a contact , send mail .
Please refer to this for more info :http://developer.android.com/guide/components/intents-filters.html
Events and Intents are different all together , all events generated by your app will go the event handler queue of the UI thread .
So in order to intercept you should set appropriate event listeners to your activity components .
Try using onTouch instead .
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}`
I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app.
I managed to disable it for all my activities by adding the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown = " + keyCode);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
return super.onKeyDown(keyCode, event);
}
But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!
I know it is possible as 'Volume Locker' app doing similar stuff.
It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.
Based on the description given for that app (note: I've never used it personally)
Prevent accidental changes to your volume settings, install Volume Locker today.
This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.
I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.