2

I have developed and app that use camera hardware on Samsung Galaxy J7 to measure light and adjust display brightness when home screen was pressed. But I got problem that when home screen was double pressed, as I set short cut for opening build-in camera app. The camera got black screen (camera hardware not work). I would like to make my app to be able to detect home screen double pressed so that it can skip using camera hardware and let build-in camera app work properly.

PS. Do not advise me to use 'long press' or 'recent app key' , because I have already used them. ^^

With code would be good. Thank you in advance.

This is option 1 code base on @saeed suggestion: it work but not absolutely perfect.

if(firstHomePressed == true){

                        firstHomePressed = false;
                        secondHomePressed = false;

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if(secondHomePressed == false){
                                    Toast.makeText(mContext,"Pressed Once",Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    Toast.makeText(mContext, "Double Pressed", Toast.LENGTH_SHORT).show();
                                }
                                firstHomePressed = true;
                            }
                        }, 1000);
                    }
                    else{
                        secondHomePressed = true;
                        Toast.makeText(mContext, "secondHomePressed : true", Toast.LENGTH_SHORT).show();
                    }

I figure out that by using "Intent.ACTION_CLOSE_SYSTEM_DIALOGS" to detect home key press action as I use, double home button presses will invoke the receiver only one time (even double press on home button).

if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
    {
        String reason = intent.getStringExtra(SYSTEM_REASON);

        //Toast.makeText(context,"ACTION_CLOSE_SYSTEM_DIALOGS : Reason : " + reason ,Toast.LENGTH_LONG).show();

        // Detect home screen key press or "recent app" key pressed when screen is in unlocked state
        if (reason != null)
        {
            MainActivity.logForDebug("MyThread", "reason : " + reason);

            if (reason.equals(SYSTEM_HOME_KEY)) {

            }
        }
    }

Anyway I got the solution. I put more delay before my app start when single or double home button pressed, to let default camera app start before my app. Then default camera can work finely.

However still do not know how to detect double pressed on home button correctly. ^^

Johan
  • 74,508
  • 24
  • 191
  • 319

3 Answers3

1
public class MyView extends View {

    GestureDetector gestureDetector;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
                // creating new gesture detector
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    // skipping measure calculation and drawing

        // delegate the event to the gesture detector
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return gestureDetector.onTouchEvent(e);
    }


    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        // event when double tap occurs
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();

            Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");

            return true;
        }
    }
}

Source

Community
  • 1
  • 1
David
  • 306
  • 6
  • 20
1

You can use a Handler to solve this particular issue. At first, you must declare private boolean doublePressedOnce = false; and then place this code in your onClick method:

if (doublePressedOnce) {
    // here you can write method to call camera app
Toast.makeText(mContext, "Double Pressed", Toast.LENGTH_SHORT).show();
}

this.doublePressedOnce = true;
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        doublePressedOnce = false;
    }
}, 2000);
saeed
  • 1,935
  • 1
  • 18
  • 33
0

You can use gesture or a timer I guess, not too much choice that I know: how to implement double click in android

Community
  • 1
  • 1
king
  • 507
  • 1
  • 4
  • 17