I am trying to write a small app that will catch the KeyEvent of the back button been pressed, when the screen is locked.
I saw that you can easily override onKeyDown or onBackPressed in order to catch this event, but this will work only if the activity is running.
As I understand on some of the android phones, if not on all of them, the OS doesn't listen to the back and menu buttons while the screen is locked.
EDIT
I have read couple of posts about capturing the event of the user pressing the volume buttons, and all says their isn't a way to do so when the screen is off.
Is their a reason to belive the case is change regarding the back button?
Check volume button usage when screen is off
Detect Volume Button Press when Screen off
Detect Hardware Volume Button Clicks When Screen Is Off
1)Register with BroadCast Receiver for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON
2) Check if Screen is locked using Keyguard Manager like :
KeyguardManager kManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isLocked = kManager.inKeyguardRestrictedInputMode();
or without using BroadCast Receiver, you can check like :
KeyguardManager kManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( kManager .inKeyguardRestrictedInputMode()) {
//locked
} else {
//not locked
}
Next Check for back Button Press event if screen is locked.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//Back Button is pressed
}
return super.onKeyDown(keyCode, event);
}
Related
I know selfie stick take picture using volume button . I have a screen recording application . While recording if i open device's existing camera application i want to take picture from camera application automatically . I can press volume button automatically by using below code :
#Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_VOLUME_UP);
} catch (Exception e) {
}
}
}).start()
It presses volume button while the camera app is not open . But when the camera app is open it shows :
java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
I know INJECT_EVENTS permission is only work with system apps and without system app it can be used in only rooted device . But while recording i can take picture with selfie stick . Is there any way to act as selfie stick while camera open from my application (Like press volume button ) . Or any other way ?
Thanks in advance
Implement OnTouchListener with your activity
public class YourActivity extends Activity implements OnTouchListener {
.....
Override onKeyDown and write your action inside it
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_UP)){
//Write your camera capture action
}
return true;
}
I am using Android's MediaPlayer in my RingtoneService (which is a background Service for Ringtone in my application)to ring a ringtone when my Alarm goes on and off!
on the start of Ringtone Service for alarming
MediaPlayer mediaSong;
mediaSong = MediaPlayer.create(this, R.raw.xyz);
mediaSong.start();
on stop after some time
mediaSong.stop();
Its perfect in ringing and stopping the tone.whereas, I would like to make my device silent or say mute when I click on hardware's volume button pressed when an alarm is still ringing.
some stated solutions didn't answer my requirement.Any help is appreciated, please.
Please let me know somewhere if I am unclear!
Thanks in advance!
You need to override onKeyDown method in your Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
mediaSong.stop
return true;
}
return super.onKeyDown(keyCode, event);
}
i am developing an app which listen volume events whenever hardware volume button is pressed. app can be in foreground or background. i have followed [this] (Is there a broadcast action for volume changes?) but its not working correctly. following issues i am facing
1) If I press the volume button once - the event is triggered 4-6 times.
2) If current volume is maximum and i increase the volume then event doesn't fire..
Please help me.
Try to use following code -
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
for background - any way to detect volume key presses or volume changes with android service?
On a rooted device, you can use the Xposed framework to hook yourself into PhoneWindowManager. A good example is the Xposed Torch Module. Just decompile it and see how they do things.
You can detect this by polling AudioManager for current volume, it's not nice solution, but i don't know better.
private Handler handler;
public int volume;
private Runnable volumeUpdater = new Runnable() {
private int updatesInterval = 100;
#Override
public void run() {
if(volume != audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)){
//volume changed put logic here
}
handler.postDelayed(volumeUpdater, updatesInterval);
}
};
I want to use my own camera modul at the Samsung Galaxy Camera EK-GC200.
I can get a keycode for both buttons, but capture button always opens his own camera intent which then of course collapes with my own camera modul.
Also zoom buttons always show some slide-popup when used.
Meanwhile I found some topics that some people were able to block the HOME button on their devices. But seems this is not usable for the camera buttons.
So is there any way to block the hardware buttons so at least the camera capture button doesn't open its own camera intent anymore ?
In your MainActivity.java (or some other activity), paste the following:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.e(TAG, "keyCode: " + keyCode); // If you want to see the keycodes
// If User hits the (physical) shutter button of the EK-GC200 camera
if (KeyEvent.KEYCODE_FOCUS == keyCode || KeyEvent.KEYCODE_CAMERA == keyCode) {
// Do nothing or start your own camera App
return true;
}
return super.onKeyDown(keyCode, event);
}
If you also want to intercept the return button, do:
if ((keyCode == KeyEvent.KEYCODE_BACK )) {
// Upon return / back key:
// Do NOT go to super.onKeyDown(keyCode, event);
return true;
}
The HOME button can not be intercepted in this manner.
Hope this helps.
I have seen couple of similar problems with solutions, but I couldn't find one that would work in my situation.
I am making VolumePreference (extending DialogPreference) that let's user choose volume level for some alarm.
In other preference user chooses desired ringtone that is played during alarm. It is also played while user is choosing volume in VolumePreference, so he knows how it actually sounds.
In yet another preference user chooses if alarm should "override" phone's media volume level when playing - I do that, so if user wants to have fixed volume level for alarm, then it shouldn't be affected by changes made by volume keys and so on.
If user chooses to do that, before starting to play alarm in AlarmActivity, I set volume to max level with AudioManager and intercept all keyDown events of volume keys, restoring volume level after alarm finishes.
Problem is, I can't block volume keys within my VolumePreference as there is no onKeyDown method.
After some checking, I found registerMediaButtonEventReceiver method of AudioManager that "Register a component to be the sole receiver of MEDIA_BUTTON intents.", which I believe could help in my situation (making some empty receiver), and even make volume locking more universal (register when I want to start lock, unregister after unlock), but it is working from API8, while I am making app for API7 - which still hold over 10% of market from what I read, so I would like to stick to it.
Any ideas on how one could block volume changes in PreferenceDialog?
After some thinking solution proved to be really simple - one can override onKeyDown method of View created in onCreateDialogView of DialogPreference or set onKeyListener of that View.
First example:
LinearLayout layout = new LinearLayout(mContext)
{
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) return true;
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) return true;
return super.onKeyUp(keyCode, event);
}
};
Second example (mDialogView is saved reference to layout from first example):
mDialogView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return true;
return false;
}
});
I have chosen second solution, as I think it is more flexible for two reasons at least
I can just remove listener to stop blocking volume keys, while in first method I can't
in first solution I need to decide if I want to block keys input while creating View - not much use if VolumePreference is extending some other Preference that shouldn't block keys input