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;
}
Related
I am developing music play app. When user push my app to background song is playing fine. Now if user opens the camera and starts recording video from camera, I need to pause the song playing from my app .How to do this?
I expect that the app responsible for video recording will request audio focus to notify other apps that they should cease playback. If this is the case,
you can implement AudioManager.OnAudioFocusChangeListener like this:
#Override
public void onAudioFocusChange(int focusChange)
{
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// pause playback
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(this);
doStopPlayback();
}
// else if... listen to other types of audio focus loss/ gain
}
where doStopPlayback() is your method for releasing the MediaPlayer etc.
See also this guide for media playback.
You can check it using method Camera.open(cameraId).
Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.
Throws RuntimeException
If opening the camera fails (For Example, if the camera is in use by another process or device policy manager has disabled the camera).
Update:
Example:
public boolean isCameraUsebyApp() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (camera != null) camera.release();
}
return false;
}
You can use this method to use as but keep this thing in mind that this method first acquire the camera.
If its acquire successfully then its mean that no other application is using this camera and don't forgot to release it again otherwise you will not able to acquire it again.
Its its throws RuntimeException it means that camera is in use by another process or device policy manager has disabled the camera.
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 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);
}
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 developed a game using andengine
I am facing a problem when using the power Button. If I press the POWER Button while playing the game the screen turns off and the onPause() is called as expected. But when I press the HOME button or POWER button again to turn on the screen, onResume() method is called and but the lockscreen shows up.
In the onResume() method, I resume the music of the game.
So as a result lockscreen shows up , but the game music plays in the background. I do not want to play the music in the lockscreen. Please help me solving this.Thank you
The solution is here: Activity handle when screen unlocked
By registering a BroadcastReceiver filtering the Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_PRESENT actions, you will be able to handle these 3 cases:
Intent.ACTION_SCREEN_OFF: When the POWER button is pressed and the screen goes black.
Intent.ACTION_SCREEN_ON: When the POWER button is pressed again and lockscreen is showed up.
Intent.ACTION_SCREEN_PRESENT: When you pass the locksreen and are back in your game.
However, in my case (using a Galaxy S GT I9000 with Froyo 2.2) these actions are not called when dealing with the HOME button (and I think it's a general behavior).
One simple and quick (but maybe not the best) way to handle both HOME and POWER buttons to pause and resume your music could be to keep the onPause and onResume methods and use a simple boolean flag like this:
private boolean mPowerButton = false;
#Override
public void onPause() {
super.onPause();
// Pause your music
Log.d("Game activity", "Music paused");
}
#Override
public void onResume() {
super.onResume();
if (!this.mPowerButton) {
// Resume your music
Log.d("Game activity", "[HOME button] Music resumed inside onResume");
}
}
public class receiverScreen extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// This is the lockscreen, onResume has been already called at this
// step but the mPowerButton boolean prevented the resumption of music
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
LevelActivity.this.mPowerButton = true;
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
// Resume your music
Log.d("Game activity", "[POWER button] Music resumed inside ACTION_USER_PRESENT action");
LevelActivity.this.mPowerButton = false;
}
}
}
Hope it helps!