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.
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 have an app using firebase scanning to scan barcodes. I want to disable the use of volume up/down to open the scanner/camera. I also want the user to be able to still change the volume using those button.
I tried overwriting the onKeyDown event on my base activity but it doesn't fix my issue.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}
I place a fragment with a webview widget to load my webpages and create a host fragmentActivity as my webpages' container. Now that there is a page, including a media player, seems like to support various of content for us. Here comes the problem, if I click the play button, the media player works well itself, however, when i adjust the power of volume, either higher or lower, the host activity finish unexpectedly. Complementally, I just handle the back event in the method of onKeyEvent.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(webview.canGoBack()){
webview.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Anybody helps me?
public boolean onKeyDown(int keycode,KeyEvent event)
{
//check if the camera button is pressed
if(keycode==KeyEvent.KEYCODE_CAMERA)
{
//if result
if (mResultView.IsShowingResult)
{
mResultView.IsShowingResult = false;
}
else if (mCameraReadyFlag == true)//switch to camera view
{
mCameraReadyFlag = false;
mPreview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
return true;
}
return super.onKeyDown(keycode, event);
}
I have the following code to check if the camera button is checked and if it is,then take a picture.But it uses the physical camera button code.I want to take a picture if the screen is pressed since camera buttons are now out of fashion.Can you give me equivalent function for the same please.
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